缩略图图像交换在莫斯莫夫上

发布于 2025-02-05 06:07:18 字数 1778 浏览 2 评论 0原文

我很想拥有一个带有多个缩略图的画廊网站,其中包括3张图像,这些图像在我对每个缩略图莫斯莫夫时就会旋转。

我在@matthias_h的另一个线程中发现了这个令人惊叹的响应式小提琴,而在缩略图盒上进行了多个缩略图图像。

http:

他的小提琴

<article>
<figure data-image-list="http://dummyimage.com/200x200/000000/cccccc.png&text=img1,http://dummyimage.com/200x200/dedede/cccccc.png&text=img2,http://dummyimage.com/200x200/b31031/cccccc.png&text=img3">
    <img class="imageHolder" data-src="http://dummyimage.com/200x200/cdcdcd/dddddd.png&text=+" src="http://dummyimage.com/200x200/cdcdcd/dddddd.png&text=+" />
</figure>

http://jsfiddle.net/matthias_h/matthias_h/2f2tth47/2f2tth47/

$(".imageHolder").mousemove(function (event) {
var xPos = event.pageX,
    imgPos = $(".imageHolder").offset().left,
    imgWidth = $(".imageHolder").width();
console.log("xPos: ", xPos, ", imgPos: ", imgPos, ", imgWidth: ", imgWidth);
var change1 = imgPos,
    change2 = imgPos + imgWidth / 3,
    change3 = imgPos + 2 * imgWidth / 3;
console.log("change1: ", change1, "change2: ", change2, "change3: ", change3);
$images = $("figure").data("imageList");

var array = $images.split(',');
if (xPos > change1) {
    $("img").attr("src", array[0]);
}
if (xPos > change2) {
    $("img").attr("src", array[1]);
}
if (xPos > change3) {
    $("img").attr("src", array[2]);

}});$("img").mouseout(function (){$("img").attr("src", $("img").data("src"));});

/ 这是两个问题。

  1. 复制它时,在任何一个上移动都会切换 在其他图像上。
  2. 它采用单个图像而不是通过CSS的BG位置,这是效率较低的,因为我个人在所有工作中都使用图像精灵。

我在HTML/CSS方面有很好的经验,但没有JavaScript的经验。很想获得一些见识,我如何解决这两个问题。主要是第一个制作带有多个图像的画廊的图库。

提前致谢!

I would love to have a gallery website with multiple thumbnails including 3 images that rotate as soon as I mousemove over each one.

I've found this amazing responsive fiddle in another thread from @matthias_h going through multiple thumbnail images while mousemove over the thumbnail box.

His fiddle: http://jsfiddle.net/matthias_h/2f2tth47/

The HTML

<article>
<figure data-image-list="http://dummyimage.com/200x200/000000/cccccc.png&text=img1,http://dummyimage.com/200x200/dedede/cccccc.png&text=img2,http://dummyimage.com/200x200/b31031/cccccc.png&text=img3">
    <img class="imageHolder" data-src="http://dummyimage.com/200x200/cdcdcd/dddddd.png&text=+" src="http://dummyimage.com/200x200/cdcdcd/dddddd.png&text=+" />
</figure>

The Javascript

$(".imageHolder").mousemove(function (event) {
var xPos = event.pageX,
    imgPos = $(".imageHolder").offset().left,
    imgWidth = $(".imageHolder").width();
console.log("xPos: ", xPos, ", imgPos: ", imgPos, ", imgWidth: ", imgWidth);
var change1 = imgPos,
    change2 = imgPos + imgWidth / 3,
    change3 = imgPos + 2 * imgWidth / 3;
console.log("change1: ", change1, "change2: ", change2, "change3: ", change3);
$images = $("figure").data("imageList");

var array = $images.split(',');
if (xPos > change1) {
    $("img").attr("src", array[0]);
}
if (xPos > change2) {
    $("img").attr("src", array[1]);
}
if (xPos > change3) {
    $("img").attr("src", array[2]);

}});$("img").mouseout(function (){$("img").attr("src", $("img").data("src"));});

Unfortunately there are two issues with this.

  1. When duplicating it, moving over any of them also switches the
    images on the other ones.
  2. It takes single images instead of bg positions via css which is less efficient since I'm personally using image sprites for all my work.

I have good experience with html/css but no experience with javascript. Would love to get some insight how I can fix these two issues. Mainly the first one to make a gallery with multiple images work in the first place.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女中豪杰 2025-02-12 06:07:18

现在您重复了这些元素,它是针对集合而不是一个元素的目标。

通常,jQuery方法的 setter 适用于所有匹配元素, getter 返回第一个匹配元素的值。

问题1:
使用
$(this)而不是$(“ img”)
$(this)而不是$(“。imageHolder”)
$(this).parent(“ figig”)而不是$(“ Figie”)

问题#2:
您将必须使用.css()来更改背景位置而不是更改HREF。
那将是$(this).css({background-position:newPosition})其中newposition必须定义...

codepen

Now that you duplicated the elements, it is targeting a collection instead of just one element.

In general, jQuery method's setter applies to all matching elements, and getter return a value for the first matching element.

Question #1:
Use
$(this) instead of $("img")
$(this) instead of $(".imageHolder")
$(this).parent("figure") instead of $("figure")

Question #2:
You will have to use .css() to change the background position instead of changing the href.
That would be something like $(this).css({background-position: newPosition}) where newPosition has to be defined...

CodePen

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文