jquery - Angular - 动态设置 html img 宽度和高度

标签 jquery html angularjs dynamic attributes

我想使用 jQuery BeforeAndAfter我的 angularJS 应用程序中的插件。但我有几张不同尺寸的图像。我尝试动态设置宽度和高度,但它不起作用。像这样:

        <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>

只有当我对这些属性进行硬编码时它才有效。我怎样才能弄清楚?

最佳答案

使用ng-style指令。

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

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'
        });
      });     
    }
  ]);
<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>

仅供引用(已更新):它无需预加载图像即可工作,但插件可以从这些图像中捕获宽度和高度。

关于jquery - Angular - 动态设置 html img 宽度和高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34710610/

相关文章:

javascript - jQuery 无法使用 .parent() 获取父元素

javascript - 放置 JS 后导航器停止工作

javascript - AngularJS 集中数据和 Restful 服务

javascript - 简单的 Jquery 对话框,但现在点击

javascript - 如何在按钮上添加 60 秒后自动点击功能并使用 JavaScript 执行该操作?

html - CSS - 如何在 div 中滚动列表

javascript - 指向 pdf 文件特定页面的 HTML 超链接

angularjs - smart-table - 如何重置过滤器集合?

Angularjs - 将参数传递给指令

javascript - 使用 Vanilla JS 监听 jQuery 事件