Javascript jquery淡出和单击功能
希望你们都做得很好 所以我有一个没有使用任何插件的图库Javascript jquery淡出和单击功能,javascript,jquery,html,css,Javascript,Jquery,Html,Css,希望你们都做得很好 所以我有一个没有使用任何插件的图库 $(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', functio
$(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);
});
});
$(文档).ready(函数(){
var指数=1;
var images=['1.jpg','2.jpg','3.jpg'];
变量caption_text=[“title1”、“title2”、“title3”];
函数rotateImage()
{
$(“#gallery”).fadeOut('fast',function()
{
$(this.attr('src',''+images[index]);
$('span').text(标题文本[索引]);
$(this.fadeIn('fast',function())
{
if(index==images.length-1)
{
指数=0;
}
其他的
{
索引++;
}
});
});
}
设置间隔(旋转图像,2500);
$('.thumb')。在('click',function()上{
var img=$('“+imageTitle+”);
设置间隔(旋转图像,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>
“height='300'class='highlight\u img'/>
标题1
“高度class='75'标题class='title1'/>
“高度class='75'标题class='title2'/>
“高度class='75'标题class='title3'/>
现在,当页面加载时,我可以毫无问题地进行图像旋转。此外,当我单击图像缩略图时,#gallery div也会根据我单击的缩略图更改图像
但是,当我调用单击时的缩略图函数时,rotateImage()不再工作,我需要刷新页面以使图像再次旋转
我应该如何编写代码来执行此操作
谢谢大家!
编辑:
抱歉说不清楚
我的问题是,我把“设置间隔(旋转图像,5000);“在.thumb on 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>
标题1
这个问题你不会得到太多答案。没有人会为你编写代码。他们会帮助你找到bug,所以请将你的问题改为
如何避免这个问题
或类似的问题…@Dwza嗨,我编辑了我的问题。