jquery 翻转混乱
试图让我的翻转改变翻转时的 src 。虽然有一个错误,但工作正常。单击缩略图后,src 有时可能包含错误的 src(即使在鼠标移开时,翻转状态仍然存在)。 。要找到错误,请单击几个缩略图并将鼠标悬停在几个缩略图上,您应该会看到已单击的缩略图保留了翻转源。演示不再可用,抱歉!
jQuery -
function image_gallery (){
if ($('ul.thumbs').length > 0) {
$('.gallery').each(function(){
$('ul.thumbs li img:gt(0)').addClass('unselected');
$('ul.thumbs li img:eq(0)').addClass('selected');
function mouse_overs () {
var unselected = $('li img.unselected');
unselected.hover(function(){
var thumb = $(this);
thumb.attr('src',thumb.attr('src')
.replace(/([^.]*\d)\.(.*)/, "$1r.$2"));
}, function(){
var thumb = $(this);
thumb.each(function(){
$(this).attr('src',$(this)
.attr('src').replace('r.jpg','.jpg'));
});
});
};
mouse_overs();
var img_main = $(this).find('img.main:first');
$(this).find('ul.thumbs img').each(function(){
$(this).click(function(){
var thumb = $(this);
var src = thumb.attr('src');
if ( src.indexOf('r.jpg') == -1) {
$(this).attr('src',thumb.attr('src')
.replace(/([^.]*)\.(.*)/, "$1r.$2"));
}
var selected = $('ul.thumbs li img.selected');
// previous img remove r.jpg
selected.attr('src',selected.attr('src')
.replace('r.jpg','.jpg'));
selected.removeClass('selected');
selected.addClass('unselected');
//current thumb add class "selected", remove "unselected"
thumb.addClass('selected');
thumb.removeClass('unselected');
mouse_overs();
var rel = $(this).parent('a').attr('rel');
img_main.fadeOut(500, function(){
img_main.attr('src', rel);
img_main.fadeIn('slow');
});
thumb.mouseout(function(){
var src = $(this).attr('src');
if ( src.indexOf('r.jpg') == -1) {
$(this).attr('src',thumb.attr('src')
.replace(/([^.]*)\.(.*)/, "$1r.$2"));
}
else return false;
});
});
});
});
}
}
HTML:
<div class="gallery">
<img class="main" src="images/gallery/yes-campaign/NL1.jpg"/>
<ul class="thumbs">
<li><a rel="images/gallery/yes-campaign/NL1.jpg"><img src="images/thumbs/yes-campaign/NL-1r.jpg"/></a></li>
<li><a rel="images/gallery/yes-campaign/NL2.jpg"><img src="images/thumbs/yes-campaign/NL-2.jpg"/></a></li>
<li><a rel="images/gallery/yes-campaign/NL3.jpg"><img src="images/thumbs/yes-campaign/NL-3.jpg"/></a></li>
<li><a rel="images/gallery/yes-campaign/NL4.jpg"><img src="images/thumbs/yes-campaign/NL-4.jpg"/></a></li>
</ul>
</div>
此 HTML 在整个页面中重复多次。翻转状态为 NL1r.jpg、NL2r.jpg 等。图像组织在文件夹中,因此所有图像文件名都使用相同的命名约定。
Trying to get my rollovers to change src on rollover. This is working ok, though there is a bug. After I have clicked on a thumbnail, the src can sometimes contain the wrong src (the rollover state remains even on mouseout). . To find the bug, click on a few thumbnails and mouseover a few, you should see the rollover src remain for ones that have been clicked already. Demo is no longer available, sorry!
The jQuery -
function image_gallery (){
if ($('ul.thumbs').length > 0) {
$('.gallery').each(function(){
$('ul.thumbs li img:gt(0)').addClass('unselected');
$('ul.thumbs li img:eq(0)').addClass('selected');
function mouse_overs () {
var unselected = $('li img.unselected');
unselected.hover(function(){
var thumb = $(this);
thumb.attr('src',thumb.attr('src')
.replace(/([^.]*\d)\.(.*)/, "$1r.$2"));
}, function(){
var thumb = $(this);
thumb.each(function(){
$(this).attr('src',$(this)
.attr('src').replace('r.jpg','.jpg'));
});
});
};
mouse_overs();
var img_main = $(this).find('img.main:first');
$(this).find('ul.thumbs img').each(function(){
$(this).click(function(){
var thumb = $(this);
var src = thumb.attr('src');
if ( src.indexOf('r.jpg') == -1) {
$(this).attr('src',thumb.attr('src')
.replace(/([^.]*)\.(.*)/, "$1r.$2"));
}
var selected = $('ul.thumbs li img.selected');
// previous img remove r.jpg
selected.attr('src',selected.attr('src')
.replace('r.jpg','.jpg'));
selected.removeClass('selected');
selected.addClass('unselected');
//current thumb add class "selected", remove "unselected"
thumb.addClass('selected');
thumb.removeClass('unselected');
mouse_overs();
var rel = $(this).parent('a').attr('rel');
img_main.fadeOut(500, function(){
img_main.attr('src', rel);
img_main.fadeIn('slow');
});
thumb.mouseout(function(){
var src = $(this).attr('src');
if ( src.indexOf('r.jpg') == -1) {
$(this).attr('src',thumb.attr('src')
.replace(/([^.]*)\.(.*)/, "$1r.$2"));
}
else return false;
});
});
});
});
}
}
The HTML:
<div class="gallery">
<img class="main" src="images/gallery/yes-campaign/NL1.jpg"/>
<ul class="thumbs">
<li><a rel="images/gallery/yes-campaign/NL1.jpg"><img src="images/thumbs/yes-campaign/NL-1r.jpg"/></a></li>
<li><a rel="images/gallery/yes-campaign/NL2.jpg"><img src="images/thumbs/yes-campaign/NL-2.jpg"/></a></li>
<li><a rel="images/gallery/yes-campaign/NL3.jpg"><img src="images/thumbs/yes-campaign/NL-3.jpg"/></a></li>
<li><a rel="images/gallery/yes-campaign/NL4.jpg"><img src="images/thumbs/yes-campaign/NL-4.jpg"/></a></li>
</ul>
</div>
This HTML is repeated various times throughout the page. The rollover states are NL1r.jpg, NL2r.jpg etc. The images are organized in folders, so all image filenames use the same naming convention.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以建议使用以下代码代替您的代码吗?
我已经用不透明度更改替换了悬停时的图像交换,这减少了服务器负载,但您可以轻松地用 src 交换替换它们。您将需要使用仅以“r.jpg”结尾的图像。
我还提取了一些配置变量,以便您可以尝试一下。
Can I suggest the following code instead of yours?
I've replaced your image swap on hover with an opacity change which cuts down on server load but you could easily replace these with src swapping. you will need to use the images with the "r.jpg" ending only.
I've also pulled out some config variables so you can play around with things a bit.
虽然我认为 Matt 的脚本做得非常出色(+1),但我仍然建议使用 GalleryView 插件< /a> (此处演示)。
我还在学习,所以我下面的假设可能不正确,所以如果我错了,请随时纠正我。但在再次查看您的代码后,我想添加这些注释:
Although I think Matt did a terrific job with the script (+1 for that), I'd still recommend using the GalleryView plugin (Demo here).
I'm still learning as well, so I may not be correct in my assumptions below so feel free to correct me if I'm wrong. But after taking another look at your code, I wanted to add these comments: