使用 jQuery 创建背景图像循环

发布于 2024-12-24 19:52:17 字数 332 浏览 0 评论 0原文

在一个网站上工作,客户希望网站的“背景图像”循环显示 3 张图像。

现在我之前已经创建了循环(或安装了插件)来循环浏览 img 标签...但没有制作一个 jQuery 函数来联系 div 的 css 并为其提供一个新图像以每 7 秒“淡出”一次

...有人知道如何做到这一点吗???


现在我想让图像作为 div 中的 img 标签淡入淡出,但不幸的是,在这种情况下无法做到这一点,因为具有不断变化的背景图像的 div 的大小是可调整的,并且图像比它宽得多,需要定位在它的中心 - 使用 css image-position:center 标签...

感谢您的帮助


working on a website where the client wants the "background image" of the website to cycle through 3 images.

Now I have created cycles before (or installed plugins) to cycle through img tags... but not made a jQuery function that contacts the css of a div and gives it a new image to "fade" to every 7 seconds...

Does anyone have an idea how to do this???


Now I would like to have the images fade through as img tags within a div but unfortunately this cannot be done in this case as the div that has the changing background images is resizable, and has images far wider than it, that need to be positioned in the center of it - hense using the css image-position:center tag...

thanks for the help


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲凉≈ 2024-12-31 19:52:17

我发现您不喜欢通过 div 来执行此操作,但您可以仅对背景颜色进行动画处理。
这是我的问题的解决方案。我使用了两张图片,因此您必须在数组中再添加一个网址,并将 count % 2 更改为 count % 3 以适合您的情况(使用三个不同的背景图像)。
这是小提琴:http://jsfiddle.net/5uBej/4/

来源:

< strong>JS

(function ($) {
    var images = ['http://silviahartmann.com/background-tile-art/images/fire_flame/fire_seamless_background_2.jpg', 'http://www.myinkblog.com/wp-content/uploads/2008/06/polaroid-background.jpg'],
        count = 0, 
        timeout, interval = 1500;

    $('#foo').width($(document).width()); 
    $('#foo').height($(document).height());   

    (function changeBackground() {
        timeout = setTimeout(function () {
            $('#foo').fadeOut(function () {

                if (count === 100) {
                    count = 0;
                }
                $('#foo').css('background-image', 'url(' + images[count % 2] + ')');
                $('#foo').fadeIn();
                count += 1;
                changeBackground();
            });
        }, interval);
    }());
}($));

CSS

.foo {
    position: absolute;
    background-image: url(http://www.myinkblog.com/wp-content/uploads/2008/06/polaroid-background.jpg);
    left: 0px;
    top: 0px;
    z-index: -2147483648;
}
.content {
    background: #ccc;
    margin: auto;
    margin-top: 10px;
    width: 350px;
    height: 350px;
    z-index: 100;
    padding: 10px;
    border: 1px solid black;
}
h1 {
    margin: auto;
    margin-bottom: 10px;
    text-align: center;
    font-size: 30px;    
}

HTML

<div class="foo" id="foo"></div>

<div class="content">
    <h1>Sample header</h1>
    Sample content here...
</div>

我希望这些代码行对您有所帮助:-)。

此致。

I saw that you don't prefer to do this by a div but you can animate just the background color.
Here is my solution of the problem. I've used two images so you've got to add one url more into the array and change count % 2 to count % 3 to fit into your case (with three different images for background).
Here is the fiddle: http://jsfiddle.net/5uBej/4/

And the source:

JS

(function ($) {
    var images = ['http://silviahartmann.com/background-tile-art/images/fire_flame/fire_seamless_background_2.jpg', 'http://www.myinkblog.com/wp-content/uploads/2008/06/polaroid-background.jpg'],
        count = 0, 
        timeout, interval = 1500;

    $('#foo').width($(document).width()); 
    $('#foo').height($(document).height());   

    (function changeBackground() {
        timeout = setTimeout(function () {
            $('#foo').fadeOut(function () {

                if (count === 100) {
                    count = 0;
                }
                $('#foo').css('background-image', 'url(' + images[count % 2] + ')');
                $('#foo').fadeIn();
                count += 1;
                changeBackground();
            });
        }, interval);
    }());
}($));

CSS

.foo {
    position: absolute;
    background-image: url(http://www.myinkblog.com/wp-content/uploads/2008/06/polaroid-background.jpg);
    left: 0px;
    top: 0px;
    z-index: -2147483648;
}
.content {
    background: #ccc;
    margin: auto;
    margin-top: 10px;
    width: 350px;
    height: 350px;
    z-index: 100;
    padding: 10px;
    border: 1px solid black;
}
h1 {
    margin: auto;
    margin-bottom: 10px;
    text-align: center;
    font-size: 30px;    
}

HTML

<div class="foo" id="foo"></div>

<div class="content">
    <h1>Sample header</h1>
    Sample content here...
</div>

I hope that these lines of code helped you :-).

Best regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文