What i would like to do is to show the products other versions of images on mousemove from left to right. Here's the simple html example:
<article>
<figure data-imageList="image2.jpg,image3.jpg,image4.jpg">
<img data-src="main-image.jpg" src="main-image.jpg">
</figure>
</article>
As you can see at html, the images src will be change from the data-imageList. When user mouseout from the article, the main-image.jpg will appear again. This will the default image. The function should be trigger when user only move his mouse from left to right on the current article. The other list items shouldn't be affected from the mousemove. Maybe, i should create a unique id or use the "each function" to do that. I couldn't solve that problem either anyway...
Could someone help me to solve that problem. I am really bad at javascript.
Thank you!
Just made a Fiddle with some dummy-images as data-attribute and following jQuery:
For demonstration just small images. Could be calculated properly according to the actual image size when the images should be swapped, but for demo just hardcoded the values.
For reference: http://api.jquery.com/data/
Update: As further requested the same example for responsive: Change responsive image
CSS:
I also added class
imageHolder
to the image (not necessary but too used to work with classes instead of just applying to elements) and left the console log messages in the Fiddle so it's easier to check the calculated width and positions.Another minor adjustment in provided example is to change the name of the data-attribute from
data-imageList
todata-image-list
, also of just being used to it. Reason is the naming convention that every hypheneddata-
attribute will be retrieved minus the hyphen and camelcased (the first letter after the hyphen), so data-image-list will be retrieved as $.data("imageList"). Additional reference for this here: W3C - The "data-"-attributeIf the name of the used data-attribute is already camelCased, then it's retrievable lowercased:
data-imageList="value1, value2, valu3"
->$.data("imagelist")
. Though not mentioned in the jQuery-api, description e.g. here: http://bugs.jquery.com/ticket/9066You can use a
div
and place andimg
control inside it. Now withonmouseover
ande.PageX
find thechange in width
and thewidth
ofdiv
and then usingif-else
change thesrc
attribute ofimg
. Finally usingonmouseleave
change thesrc
ofimg
to default.