简体   繁体   English

图像淡入淡出不适用于 Internet Explorer 或 Firefox

[英]Image fadeIn not working with Internet Explorer or Firefox

I have a basic sequence that fades in images.我有一个在图像中淡出的基本序列。 There are three images total and they fadeIn .总共有三张图片,它们是fadeIn I have this same method throughout this same page and it works, so I am confused why it will not work in Internet Explorer and Firefox.我在同一页面上使用了相同的方法并且它有效,所以我很困惑为什么它在 Internet Explorer 和 Firefox 中不起作用。

This is my code:这是我的代码:

//Home Img delay/fadein
$(function() {
  var oTop = $('#home-img-block-section').offset().top - window.innerHeight;
  $(window).scroll(function() {

    var pTop = $('body').scrollTop();
    console.log(pTop + ' - ' + oTop);
    if (pTop > oTop) {
      imgDelays();
    }
  });
});


// For three image block resizing

$('.home-img-block img').addClass(function() {
  return (this.width / this.height > 1) ? 'wide' : 'tall';
});

function imgDelays() {
  $('.fadeBlock1').delay(300).fadeIn(500);
  $('.fadeBlock2').delay(600).fadeIn(500);
  $('.fadeBlock3').delay(900).fadeIn(500);
};
.home-img-block {
  width: 33.33%;
  float: left;
  display: none;
  overflow: hidden;
  position: relative;
}
<div class="home-img-block fadeBlock1"></div>

Fiddle Demo小提琴演示

Why would this not display within the browsers mentioned?为什么这不会显示在提到的浏览器中?

Problem seems to be within the scroll handler where $('body').scrollTop() is used.问题似乎出在使用$('body').scrollTop()的滚动处理程序中。 It seems to be returning a value of 0 always in Firefox and IE whereas in Chrome it returns the correct value.它似乎总是在 Firefox 和 IE 中返回 0 值,而在 Chrome 中它返回正确的值。 Due to this, the pTop is never greater than the oTop and hence the if is always falsy and so the function does not get called at all.因此, pTop永远不会大于oTop ,因此if始终为假,因此根本不会调用该函数。

Try changing it to $(document).scrollTop or $(window).scrollTop as both seems to return a value in Firefox, Chrome and IE.尝试将其更改为$(document).scrollTop$(window).scrollTop因为两者似乎都在 Firefox、Chrome 和 IE 中返回一个值。

$(function() {
  var oTop = $('#home-img-block-section').offset().top - window.innerHeight;
  $(window).scroll(function() {

    var pTop = $(window).scrollTop();
    console.log(pTop + ' - ' + oTop);
    if (pTop > oTop) {
      imgDelays();
    }
  });
});

Not all browsers have the same CSS settings, and you found a common problem, inline-block doesnt work in FF so you need to add the moz specific version of it:并非所有浏览器都具有相同的CSS设置,并且您发现了一个常见问题, inline-block在 FF 中不起作用,因此您需要添加它的 moz 特定版本:

    display: -moz-inline-stack;
    display: inline-block;

*note it should be above the 'normal' inline-block *注意它应该在“正常” inline-block之上

For IE the workaround as far as I know is:对于 IE,据我所知,解决方法是:

display: inline-block;
*zoom: 1;
*display: inline;

Here is an excellent blog post form mozilla about this: Blog Post这是一篇关于此的优秀博客文章,来自 mozilla: 博客文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM