【问题标题】:Fade In/Out on Scroll Not Working In Safari滚动上的淡入/淡出在 Safari 中不起作用
【发布时间】:2018-02-22 17:18:54
【问题描述】:

我有下面的代码,当你向下滚动时淡入图像,当你向上滚动时淡出它们:

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

任何想法为什么当我将不透明度设置为 0.4 时这在 Safari 中不起作用?

我在 Safari 10.1.2 中进行测试。

【问题讨论】:

    标签: jquery safari fadein fadeout


    【解决方案1】:

    这里只是一个建议:为什么不检查您的对象上是否存在class 并定义机器人类。如果你这样做,你可以确保你的班级对这个 opacity 道具具有交叉浏览能力。检查此https://css-tricks.com/snippets/css/cross-browser-opacity/ ...如果您这样做...您可以:

    .transparent_class {
      /* IE 8 */
      -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=40)";
    
      /* IE 5-7 */
      filter: alpha(opacity=40);
    
      /* Netscape */
      -moz-opacity: 0.4;
    
      /* Safari 1.x */
      -khtml-opacity: 0.4;
    
      /* Good browsers */
      opacity: 0.4;
    } 
    
    .visible_class {
      /* IE 8 */
      -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
    
      /* IE 5-7 */
      filter: alpha(opacity=100);
    
      /* Netscape */
      -moz-opacity: 1.0;
    
      /* Safari 1.x */
      -khtml-opacity: 1.0;
    
      /* Good browsers */
      opacity: 1.0;
    } 
    

    而且你的 JS 代码可能会检查存在的类,而不是有一个 prop。

    if (jQuery(this).hasClass("transparent_class")) {jQuery(this).addClass("visible_class", 1500).removeClass("transparent_class");}
    

    希望这对你有用。

    【讨论】:

    • 谢谢!工作了一个款待。淡入淡出不适用于 jQuery,因此我需要将其添加为 css 过渡。不适用于旧版浏览器,但我可以接受。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 2020-12-06
    相关资源
    最近更新 更多