我有以下代码,可以在向下滚动时淡入图像,在向上滚动时淡出图像:
<script>
jQuery(window).on("load",function() {
jQuery(window).scroll(function() {
var windowBottom = jQuery(this).scrollTop() + jQuery(this).innerHeight();
jQuery(".lookbook").each(function() {
/* Check the location of each desired element */
var objectTop = jQuery(this).offset().top + jQuery(this).outerHeight();
/* If the element is completely within bounds of the window, fade it in */
if (objectTop -500 < windowBottom) { //object comes into view (scrolling down)
if (jQuery(this).css("opacity")==0.4) {jQuery(this).fadeTo(1500,1.0);}
} else { //object goes out of view (scrolling up)
if (jQuery(this).css("opacity")==1.0) {jQuery(this).fadeTo(1500,0.4);}
}
});
}).scroll(); //invoke scroll-handler on page-load
});
</script>
<style>
.lookbook {opacity:0.4;}
</style>
当我在Chrome和Firefox中测试它时,它可以正常工作,但在Safari中不行。出于某种原因,如果我将不透明度更改为0,它将在Safari中工作,即。
<script>
jQuery(window).on("load",function() {
jQuery(window).scroll(function() {
var windowBottom = jQuery(this).scrollTop() + jQuery(this).innerHeight();
jQuery(".lookbook").each(function() {
/* Check the location of each desired element */
var objectTop = jQuery(this).offset().top + jQuery(this).outerHeight();
/* If the element is completely within bounds of the window, fade it in */
if (objectTop -500 < windowBottom) { //object comes into view (scrolling down)
if (jQuery(this).css("opacity")==0) {jQuery(this).fadeTo(1500,1.0);}
} else { //object goes out of view (scrolling up)
if (jQuery(this).css("opacity")==1.0) {jQuery(this).fadeTo(1500,0);}
}
});
}).scroll(); //invoke scroll-handler on page-load
});
</script>
<style>
.lookbook {opacity:0;}
</style>
我在Safari 10.1.2中测试。