gpt4 book ai didi

javascript - jquery淡出和点击功能

转载 作者:太空宇宙 更新时间:2023-11-04 13:12:26 25 4
gpt4 key购买 nike

希望你们一切都好。

所以我的画廊没有使用任何插件

 $(document).ready(function() {
var index =1;
var images = ['1.jpg','2.jpg','3.jpg'];
var caption_text = ["title1","title2","title3"];
function rotateImage()
{

$('#gallery').fadeOut('fast', function()
{
$(this).attr('src','<?php echo base_url()."assets/highlight/";?>'+ images[index]);
$('span').text(caption_text[index]);
$(this).fadeIn('fast', function()
{
if (index == images.length-1)
{
index = 0;
}
else
{
index++;
}

});
});

}
setInterval (rotateImage, 2500);

$('.thumb').on('click', function() {
var img = $('<img />', {src : this.src,
'class': 'highlight_img'
});
var imageTitle = $(this).attr("title");
$('.highlight').html(img).show();
$('.highlight').append("<br/><span class='highlight_caption'>"+imageTitle+"</span>");
setInterval (rotateImage, 5000);
});
});

这是我的html

<div class='col-md-12 highlight'>
<img id='gallery' src='<?php echo site_url('assets/highlight/1.jpg');?>' height='300' class='highlight_img'/><br/>
<span id='highlight_caption' class='highlight_caption'>title1</span>
</div>

<div class='list'>
<div><img class='thumb' src='<?php echo site_url('assets/highlight/1.jpg');?>' height='75' title='title1'/></div>
<div><img class='thumb' src='<?php echo site_url('assets/highlight/2.jpg');?>' height='75' title='title2'/></div>
<div><img class='thumb' src='<?php echo site_url('assets/highlight/3.jpg');?>' height='75' title='title3'/></div>

现在,我可以在页面加载时毫无问题地进行图像旋转。此外,当我单击图像缩略图时,#gallery div 也会根据我单击的缩略图更改图像

但是,当我在单击函数时调用缩略图时,rotateImage() 不再起作用,我需要刷新页面以使图像再次旋转。

我应该如何编写代码来执行此操作?

谢谢!

编辑:

抱歉没说清楚我的问题是,我输入了“setInterval (rotateImage, 5000);”在 .thumb 中的 click() 函数中,我知道它正在运行,因为我尝试了 console.log,并且脚本确实执行了,但为什么图像没有改变?

最佳答案

你能试试这个吗;

JQUERY

 $(document).ready(function() {
var index = 1,
images = ['300x300','290x300','280x300'],
caption_text = ['title1','title2','title3'],
timer = null;

function rotateImage () {

$('#gallery').fadeOut('fast', function() {
$(this).attr('src','http://www.placehold.it/'+ images[index]);
$('span').text(caption_text[index]);
$(this).fadeIn('fast', function() {
if (index == images.length-1) {
index = 0;
} else {
index++;
}
});
});
}
timer = setInterval (rotateImage, 2500);

$('.thumb').on('click', function() {
var $this = $(this),
img = $this.attr('src'),
imageTitle = $this.attr('title');
$('.highlight')
.children('img')
.attr('src',img)
.show()
.end()
.children('.highlight_caption')
.text(imageTitle);
clearInterval(timer);
setInterval (rotateImage, 5000);
});
});

HTML

<div class='col-md-12 highlight'>
<img id='gallery' src='http://www.placehold.it/300x300' height='300' class='highlight_img'/> <br/>
<span id='highlight_caption' class='highlight_caption'>title1</span>
</div>

<div class='list'>
<div><img class='thumb' src='http://www.placehold.it/400x400' height='75' title='title1'/> </div>
<div><img class='thumb' src='http://www.placehold.it/500x500' height='75' title='title2'/></div>
<div><img class='thumb' src='http://www.placehold.it/350x350' height='75' title='title3'/></div>

http://jsfiddle.net/DKw8D/6/

关于javascript - jquery淡出和点击功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24604227/

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