【问题标题】:Why doesn't ng-style work initially with a css transition?为什么 ng-style 最初不适用于 css 转换?
【发布时间】:2016-07-16 17:02:58
【问题描述】:

我正在尝试通过向跨度添加类和宽度或高度样式来创建缩放效果。

<span ng-class="fat_or_tall(this)"><figure><img ng-style="set_ratio(this)" draggable="true" class="thumb" id="{{tablet.created}}" title="{{ tablet.title }}" data-ng-src="{{ tablet.src }}" /></figure></span>

它在我刷新后的第一页上工作,但是当我点击其他选项卡时,它只是在大小之间跳跃,就好像 css 转换不存在一样。如果我检查一下,我发现有时样式已经呈现,但有时它们没有。在我单击选项卡并访问每个选项卡后,缩放工作正常。为什么它在第一次尝试时不起作用,我该如何解决?

上面的第一张图片注意到没有样式,并且过渡不起作用,但是在我第二次单击选项卡(第二张图片)后,样式确实显示并且过渡效果很好。

这里:http://jsfiddle.net/U3pVM/23506/(如果你点击刷新它不会,但如果你点击运行它会。样式不会渲染。为什么?)

function TodoCtrl($scope) {
		$scope.tablet = {} 

    $scope.tablet.title = 'help'
    $scope.tablet.created = Date.now()
    $scope.tablet.src = 'http://tallclub.co.uk/wp-content/uploads/2011/04/Tall-Person-1.png'
    
		$scope.set_ratio = function (it) { //Find ratio and return style so that it can be used for zoom effect. 
    
        console.log(it)
        var img = new Image();  
        img.src = it.tablet.src;
        console.log(it.tablet)

        if(img.height>img.width){
            var h = 300*(img.height/img.width)+'px'     
            return {
                "height": h
            }   
        }else{
            var w = 300*(img.width/img.height)+'px'     
            return {
                "width":w 
            }       
        }
    }  
}

function TodoCtrlTwo($scope) {
		$scope.tablet = {} 

    $scope.tablet.title = 'help'
    $scope.tablet.created = Date.now()
    $scope.tablet.src = 'http://tallclub.co.uk/wp-content/uploads/2011/04/Tall-Person-1.png'
    
		$scope.set_ratio = function (it) { //Find ratio and return style so that it can be used for zoom effect. 
    
        console.log(it)
        var img = new Image();  
        img.src = it.tablet.src;
        console.log(it.tablet)

        if(img.height>img.width){
            var h = 300*(img.height/img.width)+'px'     
            return {
                "height": h
            }   
        }else{
            var w = 300*(img.width/img.height)+'px'     
            return {
                "width":w 
            }       
        }
    }  
}
.taller img  {
    -webkit-transition: .3s ease-in-out;
    transition: .3s ease-in-out;

}
.taller img:hover  {
    -webkit-transition: .3s ease-in-out;
    transition: .3s ease-in-out;

    height: 300px !important;
}  

figure  {
	width: 300px;
	height: 300px;
	
	//height: 200px;
	margin: 1em;
	padding: 0;
	background: #fff;
	overflow: hidden;
	border: 1px solid #222;
	
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>

<div ng-app>
  <h2>ARGH</h2>
  <div ng-controller="TodoCtrl">
<span class="taller"><figure><img ng-style="set_ratio(this)" draggable="true" class="thumb" id="{{tablet.created}}" title="{{ tablet.title }}" data-ng-src={{tablet.src}} /></figure></span>

  </div>
   <div ng-controller="TodoCtrlTwo">
<span class="taller"><figure><img ng-style="set_ratio(this)" draggable="true" class="thumb" id="{{tablet.created}}" title="{{ tablet.title }}" data-ng-src={{tablet.src}} /></figure></span>

  </div> 
</div>
刷新页面,点击运行代码sn-p,鼠标悬停。图像会跳跃。然后再次单击运行代码 sn-p 并将鼠标悬停在上面。会有一个平稳的过渡。这是为什么?

【问题讨论】:

  • 你能添加任何 Plnkr/Fiddle 吗?
  • @squirrl 你能贴出'fat_or_tall(this)'的代码吗?
  • @Kailas 我在上面做了。
  • @KhalidHussain jsfiddle.net/U3pVM/23506 是。如果你刷新它不起作用。如果你点击运行它会。

标签: angularjs css-transitions ng-style


【解决方案1】:

解决方案 1:

Working Demo

在第一次加载图像时,图像的widthheight 都是0。所以else块的代码被执行,返回NaNpx

详细说明:

var w = 300*(img.width/img.height)+'px' 
var w = 300*(0/0)+'px'  // As img.width = 0, img.height = 0 
var w = 300*(NaN)+'px'
var w = NaNpx

要解决这个问题,请设置图像的初始高度:

.taller img  {
    -webkit-transition: .3s ease-in-out;
    transition: .3s ease-in-out;
    height: 500px;
}

解决方案 2:

您可以在controller 中添加以下if 块。

if(img.height===0 && img.width ===0){
            var h = '500px';     
            return {
                "height": h
            }
    }

Working Demo

希望能解决您的问题。

【讨论】:

    【解决方案2】:

    试试这个。创建 $scope.tablet ={}; object first.and 将 ng-class 更改为 class for span 标签。

    var app = angular.module("app",[])
    app.controller('ctrl',['$scope', function($scope){
       $scope.tablet ={};
      $scope.tablet.title = 'help';
        $scope.tablet.created = Date.now();;
        $scope.tablet.src = 'http://tallclub.co.uk/wp-content/uploads/2011/04/Tall-Person-1.png'
        
    		$scope.set_ratio = function (it) { //Find ratio and return style so that it can be used for zoom effect. 
        
            console.log(it);
            var img = new Image();  
            img.src = it.tablet.src;
            console.log(it.tablet)
    
            if(img.height>img.width){
                var h = 300*(img.height/img.width)+'px'     
                return {'height':h};
            }else{
                var w = 300*(img.width/img.height)+'px'     
                return {
                    'width':w 
                }       
            }
        }
    
    }]);
    .taller img  {
        -webkit-transition: .3s ease-in-out;
        transition: .3s ease-in-out;
    
    }
    .taller img:hover  {
        -webkit-transition: .3s ease-in-out;
        transition: .3s ease-in-out;
    
        height: 300px !important;
    }  
    
    figure  {
    	width: 300px;
    	height: 300px;
    	
    	//height: 200px;
    	margin: 1em;
    	padding: 0;
    	background: #fff;
    	overflow: hidden;
    	border: 1px solid #222;
    	
    }
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
    <div ng-app="app" ng-controller="ctrl">
    <div >
       <h2>ARGH</h2>
         <div>
          <span class="taller">
             <figure>
               <img ng-mouseenter="set_ratio(this)" draggable="true" class="thumb" id="{{tablet.created}}" title="{{ tablet.title }}" data-ng-  src={{tablet.src}} />
             </figure>
          </span>
       </div>
      </div>
    </div>

    【讨论】:

    • 我不这么认为。刷新您的页面,然后单击运行代码 sn-p 并将鼠标悬停在图像上。然后再次单击运行代码 sn-p 并将鼠标悬停。看到区别,因为我知道吗?刷新页面并再次单击它。它不断发生在我身上。一个跳跃,另一个平滑。那是我的确切问题。感谢您的说明。
    • 不,你注意到跳跃和平滑过渡的区别了吗?
    • jsfiddle.net/U3pVM/23506 如果你点击刷新它不会,但如果你点击运行它会。样式不会呈现。
    • jsfiddle.net/U3pVM/23518 mouseenter 工作得更好。如果你能让它更平滑一点,我会给你绿色的。谢谢你:)
    【解决方案3】:

    答案可以在这里找到。

    is there a post render callback for Angular JS directive?

    我只是把它包裹在一个 $timeout 中

    $timeout(function(){
    
        $scope.set_ratio = function (it) { //Find ratio and return style so that it can be used for zoom effect. 
            var img = new Image();  
            img.src = it.tablet.src;
            console.log(it.tablet)
    
            if(img.height>img.width){
                var h = 300*(img.height/img.width)+'px'     
                return {
                    "height": h
                }   
            }else{
                var w = 300*(img.width/img.height)+'px'     
                return {
                    "width":w 
                }       
            }
        }
    
    }, 50)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-04
      • 2017-08-01
      • 2014-04-01
      • 1970-01-01
      • 2017-12-03
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      相关资源
      最近更新 更多