javascript - 背景更改仅适用于 chrome + $.preload 不起作用

标签 javascript jquery html css google-chrome

我制作了一个脚本,每 8 秒更改一次背景图像。在 chrome 中一切正常,但在其他浏览器中却不行。 (在 Safari、Edge、IE 9 - 11 和 Mozilla 上测试)。我也有 jQuery 预加载插件,但它实际上并没有预加载图像。控制台没有显示任何错误,所以我不知道为什么它不起作用。 代码:

var c = 1,  
    nimg = $('header .background .img').attr('data-bg'),
    bgpath = $('header .background .img').css('background').match(/"(.*)"/),
    imgpath,
    imgs = [],
    startpath,
    startpoint,
    selector = 'header .background .content .text',
    time = 8000;
if (path[1] == 'diwerf') {
    startpath = '/'+path[1];
} else {
    startpath = '/templates';
};
startpoint = bgpath[1].search(startpath);
bgpath = bgpath[1].slice(startpoint);

for (var g = 1; g <= nimg; g++) {
    imgpath = bgpath.replace(/[0-9]/g, g);

    imgs.push(imgpath);
};

$.preload(imgs);

function removeText() {
    setTimeout(function() {
        $(selector).fadeOut('slow', function() {
            $(this).removeClass('animated').removeAttr('style');
        });
    }, time-600);
}

removeText();

setInterval(function() {
    if (c == nimg) {
        c = 0;
    };
    c++;
    bgpath = bgpath.replace(/[0-9]/g, c);
    $('header .background .img').css('background', 'url('+bgpath+') center center no-repeat');

    setTimeout(function() {
        $(selector+'-'+c).addClass('animated');
    }, 600);

    removeText();
}, time+100);

它什么都不做,甚至没有添加“动画”类。

在这里你可以看到网站:http://www.testing.dw-erfolg.eu/

感谢您的帮助!

最佳答案

您使用的是速记 CSS 属性,并非所有浏览器都能保证。

bgpath = $('header .background .img').css('background').match(/"(.*)"/),

您应该使用 .css('backgroundImage')

确保您还更改了第 42 行:

$('header .background .img').css('backgroundImage', 'url('+bgpath+')');

关于javascript - 背景更改仅适用于 chrome + $.preload 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38466604/

相关文章:

javascript - 如何删除元素并嵌入jointjs

php - jQuery 网络摄像头 : Save mode results in: '' error: No save mode compiled in.“

javascript - 如何在仪表事件路径上放置文本

javascript - 需要有关 pushState 和 onpopstate 的建议

javascript - 将多个值保存到 LocalStorage 中的相同类型,然后检索值

javascript - jQuery,发现 dom 对象的类是什么

php - 在处理根目录中的页面时,Bootstrap 下拉菜单因文件夹中的页面而损坏。两组 php_include 相同的外部头文件

html - 如何使背景图像连续水平滚动而不中断动画?

jquery - 悬停时多个图像变化

javascript - 在事件处理程序中访问事件对象