我有一张图片,中间有一个图标,表示可以为用户滚动。当用户滚动时,我想淡出图标。现在我可以隐藏图片,但想要淡入淡出的效果。
这是我的看法:
<div style="overflow: scroll;">
<img id="fullImage" ng-src="{{imageUrl}}" imageonload>
<img id="drag-arrows" class="drag-arrows" ng-src="images/icon-drag.png"/>
<h3 id="optionName" class="optionName">{{ optionName }}</h3>
<a class="close-reveal-modal" ng-click="cancel()"><img class="cancel-icon" ng-src="images/icon-close-fullscreen.png"></a>
</div>
这是我的指令,它监听 touchmove 事件以隐藏拖动箭头图标:
modalController.directive('imageonload', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('load', function() {
var img = document.getElementById("fullImage");
var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
var h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
var imageWidth = img.width;
var imageHeight = img.height;
var aspectRatio = imageWidth / imageHeight;
if (w < imageWidth && h < imageHeight){
// scale image down
var widthDiff = imageWidth - w;
var heightDiff = imageHeight - h;
if(widthDiff <= heightDiff){
imageWidth = w;
imageHeight = w / aspectRatio;
}
else{
imageHeight = h;
imageWidth = h * aspectRatio;
}
}
else if (w < imageWidth && h > imageHeight) {
// fill image vertically
imageHeight = h;
imageWidth = h * aspectRatio;
}
else if (w > imageWidth && h < imageHeight) {
// fill image horizontally
imageWidth = w;
imageHeight = w / aspectRatio;
}
else {
// fill image in both directions
var widthDiff = w - imageWidth;
var heightDiff = h - imageHeight;
if(widthDiff >= heightDiff){
imageWidth = w;
imageHeight = w / aspectRatio;
}
else{
imageHeight = h;
imageWidth = h * aspectRatio;
}
}
img.style.width = imageWidth + "px";
img.style.height = imageHeight + "px";
img.style.maxWidth = "none";
// add scroll left to parent container
var container = img.parentNode;
var scrollLeft = (imageWidth - w) / 2;
container.scrollLeft = scrollLeft;
});
element.bind('touchmove', function(){
var arrows = document.getElementById("drag-arrows");
arrows.style.display = "none";
});
}
};
});
如果可能的话,关于如何创建这种淡入淡出效果,尤其是正确的 angularjs 方式,有什么建议吗?
最佳答案
如果您使用的是 jQuery,请尝试 .fadeOut()
而不是 arrows.style.display = "none";
arrows.fadeOut();
如果您不使用 jQuery,那么您可以使用 CSS 类来实现:
arrows.className = "drag-arrows hidden";
在 CSS 中:
.drag-arrows {
opacity: 1;
-webkit-transition: opacity 0.5s; /* For Safari 3.1 to 6.0 */
transition: opacity 0.5s;
}
.hidden {
opacity: 0;
}
请注意,CSS 解决方案只会更改箭头的 opacity
,而不是实际的 display
属性,因为 display
不能动画。如果您仍想将箭头的显示属性设置为 none
,则需要添加一个 eventListener
,如下所示:
arrows.className = "drag-arrows hidden";
arrows.addEventListener("transitionend", function(){
arrows.style.display = "none";
arrows.removeEventListener("transitionend");
}, false);
如果你想稍后再次显示你的箭头,你可以使用:
arrows.className = "drag-arrows";
arrows.addEventListener("transitionend", function(){
arrows.style.display = "block"; // Or whichever display you had before
arrows.removeEventListener("transitionend");
}, false);
关于javascript/angularjs - 淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28810014/