我尝试通过向跨度添加类和宽度或高度样式来创建缩放效果。
<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
}
}
}
}
&#13;
.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;
}
&#13;
<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>
&#13;
答案 0 :(得分:1)
解决方案1:
首次加载图片时,图片的width
和height
均为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:
您可以在if
中添加以下controller
阻止。
if(img.height===0 && img.width ===0){
var h = '500px';
return {
"height": h
}
}
希望能解决你的问题。
答案 1 :(得分:0)
试试这个。 create $ scope.tablet = {};对象first.and将ng-class更改为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
}
}
}
}]);
&#13;
.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;
}
&#13;
<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>
&#13;
答案 2 :(得分:0)
答案可以在这里找到。
is there a post render callback for Angular JS directive?
我只是把它包裹在$ timeout
中<img ng-src="addProfileController.cropImgSrc">