首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度-动态设置html img宽度和高度

角度-动态设置html img宽度和高度
EN

Stack Overflow用户
提问于 2016-01-11 04:50:21
回答 1查看 9.5K关注 0票数 2

我想在我的angularJS应用程序中使用jQuery BeforeAndAfter插件。但我有几张不同尺寸的图片。我已经尝试动态设置宽度和高度,但它不起作用。如下所示:

代码语言:javascript
复制
        <div id="container">
            <img id="imgMY" ng-src="{{photo.source}}" alt="photo" width="{{photo.width}}" height="{{photo.height}}"/>
            <img id="imgM2" ng-src="{{photo.source}}" alt="photo" width="{{photo.width}}" height="{{photo.height}}"/>
        </div>
    ...
<script type="text/javascript">
    $(function(){
        $('#container').beforeAfter({
        animateIntro : true,
            introDelay : 1000,
            introDuration : 500,
            showFullLinks : false,
            dividerColor  : '#1481E3'
    });
    });
</script>

只有当我硬编码了这些属性时,它才能起作用。我怎么才能弄清楚呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-11 05:05:31

使用ng-style指令。

示例: <img ... ng-style="{ width : photo.width, height : photo.height }" />

代码语言:javascript
复制
angular.module('test', [])
  .controller('TestController', ['$scope', '$document', '$timeout',
    function($scope, $document, $timeout) {
      $scope.photo = {
        link: 'https://pbs.twimg.com/profile_images/2149314222/square.png',
        link2 : 'http://cdn-prod.portlandwebworks.com/sites/default/files/angular-3.jpg',
        style: {
          width: '300px',
          height: '200px'
        }
      };
      
      $timeout(function() {
        $('#container').beforeAfter({
          animateIntro: true,
          introDelay: 1000,
          introDuration: 500,
          showFullLinks: false,
          dividerColor: '#1481E3'
        });
      });     
    }
  ]);
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js"></script>
<script src="http://www.catchmyfame.com/jquery/beforeafter/js/jquery-ui-1.8.13.custom.min.js"></script>
<script src="http://www.catchmyfame.com/jquery/beforeafter/js/jquery.beforeafter-1.4.min.js"></script>
<div ng-app="test">
  <div id="container" ng-controller="TestController">
    <img ng-src="{{ photo.link }}" ng-style="photo.style" />
    <img ng-src="{{ photo.link2 }}" ng-style="photo.style" />
  </div>
</div>

仅供参考(更新): it无需预加载图片即可工作,但插件会捕捉这些图片的宽度和高度。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34710610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档