简单的 Jquery 画廊问题
我是 Jquery 新手,正在尝试构建一个简单的画廊。我知道有很多插件,但我不想使用其中任何一个。我的问题很简单。单击拇指时如何淡入图像。另外我怎样才能实现自动淡入和淡出。我将非常感谢任何回应。谢谢
这是我的代码。
//HTML
<div class="LargeImage">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<div class="thumbsImages">
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
</div>
// JavaScript
$(document).ready(function() {
var LargeImages = $(".LargeImages").children();
var SmallImages = $(".thumbsImages").children();
SmallImages.each(function() {
SmallImages.click(function() {
LargeImages.each(function() {
// I have problem here with logic
});
$(this).addClass("active");
$(this).siblings().removeClass("active");
});
});
});
I am new to Jquery and and trying to build a simple gallery. I know there is lots of plugins, but I don't want to use any of them. my question is very simple. how can I fade in image when click on thumb. also how can I achieve auto fadeIn and Out. I will really appreciate any response. thanks
here is my code.
//HTML
<div class="LargeImage">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
<div class="thumbsImages">
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
</div>
// JavaScript
$(document).ready(function() {
var LargeImages = $(".LargeImages").children();
var SmallImages = $(".thumbsImages").children();
SmallImages.each(function() {
SmallImages.click(function() {
LargeImages.each(function() {
// I have problem here with logic
});
$(this).addClass("active");
$(this).siblings().removeClass("active");
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不想在
SmallImages.each(...)
中调用SmallImages.click(...)
,您最终会挂接每个图像上多次单击
事件。 (click
将处理程序挂接到您调用它的 jQuery 实例内的所有 匹配元素。)这是一种基本方法,无需额外的
div
s:HTML:
JavaScript:
Live example
基本上,我在那里做的是存储缩略图的
img
元素中完整尺寸版本的 URL,为data-fullsize
(data-
前缀意味着该属性将在以下条件下验证) HTML5;在 HTML5 之前,没有官方方法可以拥有自己的属性,但浏览器允许它,即使它在技术上是无效的)。然后,当用户单击图像时,我们会淡出大 div 中显示的所有内容,然后将其替换为全尺寸图像并淡入。You don't want to call
SmallImages.click(...)
within theSmallImages.each(...)
, you'll end up hooking theclick
event on each image multiple times. (click
hooks the handler up to all matching elements inside the jQuery instance you call it on.)Here's a basic way to do what you're doing without the extra
div
s:HTML:
JavaScript:
Live example
Basically, what I'm doing there is storing the URL of the full size version in the
img
element for the thumbnail asdata-fullsize
(thedata-
prefix means that the attribute will validate under HTML5; prior to HTML5 there's no official way to have your own attributes, but browsers allow it even though it's technically invalid). Then, when the user clicks an image, we fade out whatever's showing in the big div, then replace it with the full-size image and fade in.您应该做什么的想法:
这不是真正优化的代码。但这很容易理解。 ;)
我不知道这是否是您所需要的,但我希望它有帮助!
An idea of what you should do:
It's not a really optimized code. But it's simple to understand. ;)
I don't know if it's what you´re needing, but I hope it helps!