简体   繁体   English

chrome上带有淡入淡出的Javascript滑块在FF和IE7 / 8/9上无法运行

[英]Javascript slider with fade fails on chrome while works on FF and IE7/8/9

I am facing problems with this javascript code. 我遇到了与此JavaScript代码有关的问题。 It does a fade every 6 seconds between background and foreground div, to create an slider. 它在背景和前景div之间每6秒钟执行一次淡入淡出操作,以创建一个滑块。 It works perfectly on Firefox 9 and probably in lower versions, and I have test that it really works in IE7, IE8 and IE9. 它可以在Firefox 9以及更低版本的Firefox上完美运行,并且我测试了它确实可以在IE7,IE8和IE9中运行。

But in chrome it just do the first fading, then the image stays static. 但是在chrome中,它只会先褪色,然后图像保持静态。 Here is the javascript code: 这是JavaScript代码:

function start_slider () {
    var imgArr = new Array( // relative paths of images
        '/solteros/img/slider/barrancos.png',
        '/solteros/img/slider/karts.png',
        '/solteros/img/slider/parque.png',
        '/solteros/img/slider/canoaraft.png',
        '/solteros/img/slider/paintball.png',
        '/solteros/img/slider/quads.png',
        '/solteros/img/slider/rafting.png'
    );

    var preloadArr = new Array();
    var i;

    /* preload images */
    for(i=0; i < imgArr.length; i++){
        preloadArr[i] = new Image();
        preloadArr[i].src = imgArr[i];
    }

    var currImg = 0;
    var intID = setInterval(changeImg, 3000);
    var hs = document.getElementById ( 'header_slider' );    
    var hsb = document.getElementById ( 'header_slider_bkg' );
    if ( hs.style.opacity != 1 ) {
         hs.style.opacity = 1; 
    }

    var foInt = null;
    var fiInt = null;
    var imgUrl = "";

    function changeImg () {
        clearInterval ( intID );
        currImg = ( currImg < preloadArr.length-1 ) ? currImg+1 : 0;
        imgUrl = preloadArr[currImg].src;
        hsb.style.background = 'url('+imgUrl+') top center no-repeat';
        foInt = setInterval ( fadeOut, 50 );
    }

    function fadeOut () {
        if ( hs.style.opacity <= 0 ) {
            clearInterval ( foInt );
            hs.style.background = 'url('+imgUrl+') top center no-repeat';
            hs.style.opacity = 1;
            hs.style.filter = 'alpha(opacity=100);'; /* Para IE8 y anteriores */
            intID = setInterval (changeImg, 6000);
        } else {
            hs.style.opacity -= 0.05;
            hs.style.filter = 'alpha(opacity='+(Math.round(hs.style.opacity * 100))+');'; /* Para IE8 y anteriores */
        }
    }
}

Now, the CSS involved: 现在,涉及的CSS:

#header_slider_bkg {
    width: 1000px;
    height: 410px;
    min-height: 410px;
    margin: 0;
    padding: 0;
    float: left;
    background: url('/solteros/img/slider/rafting.png');
}

#header_slider {
    width: 1000px;
    height: 410px;
    min-height: 410px;
    margin: 0;
    padding: 0;
    float: left;
    background: url('/solteros/img/slider/rafting.png');
}

And so, the HTML: 因此,HTML:

    <div id="header_slider_bkg">
        <div id="header_slider">
        </div>
    </div>
    <script type="text/javascript">
        start_slider ();
    </script>

The question obviusly is why this fails on Chrome while it works in every other major browsers??? 显而易见的问题是,为什么它不能在Chrome上同时在所有其他主流浏览器上运行?

TIA TIA

After installing Chrome Developer Tools and do a bit of debugging, I have found that seems for me like a bug on Chrome javascript engine. 安装Chrome开发人员工具并进行一些调试后,我发现对我来说,这似乎像Chrome javascript引擎上的错误。

The property hs.style.opacity starts with 1. The first time it does: hs.style.opacity属性从1开始。

hs.style.opacity -= 0.05

It takes an expected value of 0.95. 期望值为0.95。 But the second time it gets 0.899999999999999 instead of 0.90 as expected. 但是第二次获得0.899999999999999,而不是预期的0.90。 The loop continues until it gets a value of 0.4999...9684, then it seems to refuse to subsctract 0.05 so the fadeOut interval it is never clear because hs.style.opacity never reach a value of 0 or below. 循环一直持续到它的值达到0.4999 ... 9684,然后似乎拒绝减去0.05,因此渐变输出间隔永远都不清晰,因为hs.style.opacity永远不会达到0或以下的值。

A quick fix seems to be changing: 快速修复似乎正在改变:

if ( hs.style.opacity <= 0 )

by 通过

if ( hs.style.opacity < 0.5 )

But this shows a wider gap between images at last change, so a better solution may be to round the value to two decimals after substraction, so I try a new approach that works fine: 但这显示了最后一次更改图像之间的差距更大,因此更好的解决方案可能是在减去后将值舍入到两位小数,因此我尝试了一种行之有效的新方法:

Changing: 变更:

hs.style.opacity -= 0.5;

By: 通过:

aux = Math.round(hs.style.opacity * 100);
aux -= 5;
hs.style.opacity = aux / 100;

So this is SOLVED. 所以这已解决。

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

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