所以我有没有使用任何插件的画廊
$(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);”在click()函数里面.thumb,我知道它正在运行,因为我尝试了console.log,脚本确实执行了,但为什么图像没有改变?
答案 0 :(得分:1)
你能试试吗;
<强> 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>