简体   繁体   English

用jQuery在背景中循环

[英]Cycling through backgrounds with jquery

I use progressive CSS to style my website background images. 我使用渐进式CSS设置网站背景图像的样式。 I have found a few tutorials on cycling through images with jQuery, but I have yet to see any that will do it through CSS. 我已经找到了一些有关使用jQuery循环浏览图像的教程,但是我还没有看到任何可以通过CSS实现的教程。 All of them are with HTML. 所有这些都带有HTML。 How can I do this so that the Jquery or Javascript cycles through images without having to create a div to do so. 我该怎么做,以便Jquery或Javascript在图像之间循环,而无需创建div来这样做。

My CSS: 我的CSS:

html {
background: url('../assets/homepage.jpg') no-repeat center center fixed; 
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}

You sure can do it, but there won't be any transition when the image changes: 您确实可以做到,但是当图像改变时不会有任何过渡:

html, html.slide1 {
    background: url('../assets/homepage1.jpg') no-repeat center center fixed; 
    /* add vendor prefixes */
    background-size: cover;
}
html.slide2 {
    background-image: url('../assets/homepage2.jpg');
}
html.slide3 {
    background-image: url('../assets/homepage3.jpg');
}
$(function () {
    var classList = ['slide1', 'slide2', 'slide3'],
        $html = $('html'),
        last = classList.length - 1,
        current = 0;

    setInterval(function () {
        current = current == last ? 0 : current + 1;
        $html.prop('class', classList[current]);
    }, 1000);
});

Here's the fiddle: http://jsfiddle.net/rmR7V/ 这是小提琴: http : //jsfiddle.net/rmR7V/

Try sliding the background image off-screen, changing it and sliding it back on. 尝试将背景图像滑出屏幕,进行更改,然后再滑回。

<style>
html {
background: url('Image1.jpg') no-repeat center center fixed; 
background-size: cover;
}
</style>

<script type="text/javascript">
$(document).ready( function() {
    $('html').animate({
        "background-position": -2048
    },2000, function() {
        $('html').css('background-image',"url('Image2.jpg')");
        $('html').animate({
        "background-position": 0
        },2000);
    });
});
</script>

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

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