gpt4 book ai didi

jquery - 如何淡化循环图库背景图像

转载 作者:技术小花猫 更新时间:2023-10-29 11:02:52 27 4
gpt4 key购买 nike

我想旋转一个 div 的背景图片,我认为这会妨碍我使用出色的 jQuery Cycle 插件。

下面的代码是我目前所拥有的,但它的行为并不如我所愿,即:

  • 淡出第一张图片
  • 在不显示时用第二张图片交换图片图像 - (在第二张图像中淡入淡出失败)
  • 并重复到无限……甚至更远。

<script type='text/javascript' src='http://code.jquery.com/jquery-1.5.2.js'></script> 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="/css/normalize.css">
<link rel="stylesheet" type="text/css" href="/css/result-light.css">

<style type='text/css'>
#mydiv {

width: 100%;
height: 365px;
margin-left: auto;
margin-right: auto;
position: relative;
display: block;
background: url(HP_jQuery1.jpg) center top no-repeat;
}
</style>

<script type='text/javascript'>
$(window).load(function(){
var firstTime = true;
var arr = ["url(HP_jQuery2.jpg)", "url(HP_jQuery3.jpg)"];
(function recurse(counter) {
var bgImage = arr[counter];
if (firstTime == false) {
$("#mydiv").fadeOut("slow");
$('#mydiv').css('background-image', bgImage);
$("#mydiv").fadeIn("slow");
} else {
firstTime = false;
}
delete arr[counter];
arr.push(bgImage);
setTimeout(function() {
recurse(counter + 1);
}, 3600);
})(0);
});
</script>

</head>
<body>
<div id="mydiv">&nbsp;</div>
</body>

最佳答案

Use-case jsBin Demo

为了交叉淡化背景图片,您需要一个额外的层来覆盖您的画廊。
以下是所涉及逻辑的示例:

Fade loop background images

所以创建一个DIV 到它正常的地方<img>浏览器预加载图像的标签,我们将使用它们的 src之后按照说明使用 background-image对于两个元素:

jQuery(function($) {

var $gallery = $("#gallery"); // Your HTML gallery
var $overlay = $("<div/>"); // An overlay to X-fade
var $images = $gallery.find("img");
var n = $images.length; // How many images we have
var c = 0; // Just a counter to loop using Reminder Operator %

$gallery.css({backgroundImage: "url("+ $images[c].src +")"}).append( $overlay );

(function loopBg(){
$overlay.hide().css({backgroundImage: "url("+ $images[++c%n].src +")"}).delay(2000).fadeTo(1200, 1, function(){
$gallery.css({backgroundImage: "url("+ $images[c%n].src +")"});
loopBg();
});
}());

});
html, body{
height:100%; margin:0; /*If needed*/
}


#gallery, /*Your gallery ID */
#gallery > div /*Immediate DIV child, created by jQuery*/
{
position:absolute;
width: 100%; height:100%;
background: #000 none 50% / cover;
}
#gallery img{
display:none;
/*Exactly. We'll use them as BG images*/
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="gallery">
<!--
We'll not show images, those are just used for the browser to preload them
Let's just steal the images Sources and use them for the #gallery and #overlay backgrounds
-->
<img src="http://dummyimage.com/600x365/fc5/fff&text=Image1" />
<img src="http://dummyimage.com/600x365/cf5/fff&text=Image2" />
<img src="http://dummyimage.com/600x365/f5c/fff&text=Image3" />
</div>

关于jquery - 如何淡化循环图库背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6787359/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com