jQuery script for swapping images on hover/rollover on Drupal 7 + Views
Category:
I tried to find the way to make do the rollover image using Views but after searching and looking into many websites, it doesn't seem to have clear the instruction how to do it. So, here is what I did to make it work. First of all, you need to download and install Image URL Formatter here -> http://drupal.org/project/image_url_formatter
1. Create a new content with 2 image fields. The Regular image field and a Rollover image field.
2. Create a new view. In the FIELDS, add Rollover image field first, follow by Regular image field.
3. Configure Rollover image field
- Check on "Exclude from display"
- Set Formatter as Image URL
Rollover Image Setting Screen Capture
4. 3. Configure Regular image field as below :
- Set Formatter as Image URL
- Check on Rewrite the output of this field
- Type below HTML into the text box
<img src="[field_regular_image]" rel="[field_rollover_image]" class="rollover" />
Regular Image Setting Screen Capture
5. Now, save your views. Then, you have to create a JS file to make it work. Let's created it in your theme directory. I use Zeropoint Theme, so, I create rollover.js in _custom folder under Zeropoint directory :
6. in rollover.js, paste the code below into it :
(function ($) { $(document).ready(function() { //rollover swap images with rel var img_src = ""; var new_src = ""; $(".rollover").hover(function(){ //mouseover img_src = $(this).attr('src'); //grab original image new_src = $(this).attr('rel'); //grab rollover image $(this).attr('src', new_src); //swap images $(this).attr('rel', img_src); //swap images }, function(){ //mouse out $(this).attr('src', img_src); //swap images $(this).attr('rel', new_src); //swap images }); //preload images var cache = new Array(); //cycle through all rollover elements and add rollover img src to cache array $(".rollover").each(function(){ var cacheImage = document.createElement('img'); cacheImage.src = $(this).attr('rel'); cache.push(cacheImage); }); }); })(jQuery);
7. Now, edit "zeropoint.info" to load rollover.js. Just add below line into the file :
scripts[] = /_custom/rollover.js
It will look like below image :
That's it. Now, test your rollover image. It should work the way we want it to be.
Software Tested :
- Drupal 7.18
- Views 7.x-3.5
- Image URL Formatter 7.x-1.0
Source :
- http://grasmash.com/article/simple-jquery-script-swapping-images-hoverrollover
Comments
Phinkalily
Thu, 06/13/2013 - 20:17
Permalink
How about just using css to do the trick?
thanks for the tutorial :)
I think this one is more simpler ,I think CSS can do the trick
Goal: To make a rollover state for images in the view.
I have a regular image field and rollover image field in my views.
I just display both of them using "image" as formatter.
My arrangement of the fields are : Regular image then Rollover image
~~In my css~~
I make the regular image field "position:absolute"
so it's on the top now of the rollover image
When hover , I give the regular image field "opacity:0.1" so that the rollover image at the back will appear.
No JQuery , No other modules installed.. I think it can do the trick :)
Math
Sun, 06/29/2014 - 04:05
Permalink
CSS
Hi Phinkalily
Could you share your CSS code with us?
Many thnaks
Jake Postell
Wed, 08/07/2013 - 09:42
Permalink
Images duplicating
I currently have several images using this code on a page and when switching from one to another sometimes they duplicate to another image or switch hover states. Is there something wrong with this code and the current version of Drupal 7?
Hetal
Sun, 10/13/2013 - 07:34
Permalink
change image on link hover
I have tried above call and it's working perfectly fine. It's working on image hover. I would like to change image image on link hover. Can you suggest me any solution???
Urmila
Thu, 03/05/2015 - 01:20
Permalink
Issue in IE8 & IE9
javascript for rollover is working in other browsers but, for IE8 & IE9 it is not working. Can you suggest any solution?
Capono
Sat, 05/30/2015 - 00:20
Permalink
cache, title, alt
Nice tutorial, works like a charm. I would like to add something:
- clear cache after changing .info file (/admin/config/development/performance)
- add title and alt attributes to image tag in views (in my case it's term name)
<img src="[field_regular_image]" rel="[field_rollover_image]" class="rollover" alt="[name]" title="[name]"/>
Balakrishnan
Fri, 09/04/2015 - 00:11
Permalink
I want to Give link to Image,
I want to give a link to image, when clicking particular image then redirect to that particular content. How to do that ?
TheRangeXTDreview
Fri, 08/21/2020 - 22:01
Permalink
RangeXTD
RangeXTD has been making a considerable amount of commotion in the news space as the most recent must-purchase item. https://rangextdreview.com/ Shoppers have been hurrying to arrange the RangeXTD wifi extender before stocks run out, making statements like, "In addition to the fact that it boosted the wifi signal, it additionally sped up" and, "This lets me set up my own rapid private association.
car tools
Sun, 09/06/2020 - 05:10
Permalink
car accessories
Many car seat cushions offer the kind of ergonomics and attention to human anatomy that is sorely lacking in car seat design. As a bonus, seat cushions can also make up for deficiencies in the material your seats are made of. Some materials best car neck pillow https://autoxtools.com/best-car-neck-pillow/
stain easily, store body heat, or take forever to heat up on cold days. Others include materials or textures that grip clothing too roughly, making it difficult to slide in and out of the car without bunching up and creasing the pants or dress. With a variety of materials and options, seat cushions can be a practical alternative.
ecoSex
Sat, 09/26/2020 - 20:44
Permalink
Хорошо работаем
<b><a href=https://eco-corporation.ru></a></b>
ecoSex
Tue, 09/29/2020 - 12:10
Permalink
Всё верно мастерим
<b><a href=https://eco-corporation.ru></a></b>
PUBG hile
Mon, 10/05/2020 - 03:11
Permalink
PUBG mobile
It is great article you share for reading. Great Post !! I like your creativity. am playing the game called pubg mobo. and I have some interesting tricks about free UC and BP for PUBG
https://community.windy.com/user/carant9696
newhapyrz
Fri, 10/09/2020 - 04:07
Permalink
In the event that your
In the event that your sweetheart is an individual who energizes on perusing new things, at that point these New Year Wishes For https://happynewyear2021h.com/ Boyfriend can be sent to him. It is quite simple to wish your beau utilizing our desires. You will definitely get answers that you were unable to try and anticipate.
hiju
Tue, 10/13/2020 - 04:27
Permalink
pet insurance for dog
<A href="https://cutepetinsurance.com">do you searching for pet insurance near to you ? </a>
dfgf
Tue, 10/13/2020 - 04:28
Permalink
cheap pet insurances near to ME
do you searching for pet insurance near to you ?
Isabellgracia
Mon, 10/19/2020 - 02:19
Permalink
Internet Speed Test
hi,
I have checked so many website, your website is very amazing. you should try this website
https://uberant.com/article/900871-o-que-uma-boa-velocidade-de-download-...
Isabellgracia
Fri, 10/23/2020 - 00:11
Permalink
Booteble Pendrive
Hi there,
this is a very good blog I appreciate your work. thanks for sharing such a useful information.
visit my blog Rufus
https://rufusinfo.puzl.com
Isabellgracia
Mon, 10/26/2020 - 04:55
Permalink
Card Generator
Hi there,
this is a very good blog I appreciate your work. thanks for sharing such a useful information.
visit my blog Credit Card Generator
https://diigo.com/0hxe3k
firox
Tue, 11/10/2020 - 22:16
Permalink
bad credit finance help
if your having a problem with bad credit score and not getting installments check here for solution for your problem https://badcreditfinancehelp.com/long-term-installment/
David Levi
Mon, 11/16/2020 - 20:08
Permalink
economics homework help
Most tutors I have come across also offer economics homework help to students. Do you concentrate on the classes only or do you also offer microeconomics assignment help? I attend all my classes but still find it hard to complete the assignments. I, therefore, want help and a guarantee that I will get a good grade in the assignment.
https://www.economicshomeworkhelper.com/
David Levi
Mon, 11/16/2020 - 20:08
Permalink
Matlab assignment help
I enjoy taking Matlab assignment help from the company but I must say the former Matlab homework help experts were better. They were much easier to work with. I wish I could say the same about the writers you guys have hired these days. Immediately you place an order and make a payment, they go dark. They don’t tell you how your task is doing or whether they are having trouble completing it. Nothing! I had a control systems assignment done the other day by one of your new experts and even though it was up to the mark, the experience wasn’t what I hoped for.
https://www.matlabassignmentexperts.com/
David Levi
Mon, 11/16/2020 - 20:09
Permalink
statistics homework help
As a statistics homework help tutor, there are several statistical software that you can be proficient in. I need to know precisely Which is your preferred software for statistical analysis? Do you use Matlab? Most of my assignments are in Matlab. I need someone who can handle assignments in Matlab? Can you do my statistics homework using Matlab? More to that I would like to know whether you offer Statistics assignment help.
https://www.statisticshomeworkhelper.com/
David Levi
Mon, 11/16/2020 - 20:10
Permalink
programming assignment help
Day made. My homework was completed earlier than expected. With my final year project now complete I will be taking programming assignment help from you for my remaining assignments. In the next two days, I need a C assignment help expert to work on my assignment. I think I might go with Hayez again because he was amazing in this previous paper.
https://programminghomeworkhelp.com/
Carlos55
Tue, 11/24/2020 - 01:37
Permalink
Click Here
Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our Blog Click here :- https://bit.ly/3pV5L31
Don Strosin
Thu, 12/10/2020 - 00:20
Permalink
check
Thanks for sharing such a valuable information. also you can <a href="http://donstrosin.blogofoto.com/26311266/7-zip-free-download">check</a>
Carry Leos
Fri, 12/11/2020 - 01:05
Permalink
Check my Blog
Thank you for sharing such wonderful information with us. You can also check my blog here. Check my
https://www.goodreads.com/user/show/125745568-voice-recorder
Mario Luise
Fri, 12/11/2020 - 03:24
Permalink
Mario Luise
Wonderful article I've read at this time. Thanks for sharing this. Its helps to many members that subscribed this. You can check my blog also here.
https://voicerecorder.zohosites.in/
Levis Danial
Tue, 12/15/2020 - 02:35
Permalink
Check my blog here
Best article ever on this topic. Nice one I admire you. Thank You for sharing this with us. You can also check this.
https://makemkv.jimdofree.com/
Justin Jhoseph
Wed, 12/16/2020 - 00:40
Permalink
Justin Jhoseph
Wonderful article I've read at this time. Thanks for sharing this. You can also check my blog.
https://online-voice-recorder.yolasite.com/
Anthem Niel
Thu, 12/17/2020 - 02:49
Permalink
Visit Here
Good and useful information. Thank you for sharing. Also Check my Blog here.
https://skindex.foroactivo.com/t2-how-to-customize-a-minecraft-skin#2
Zac Hedley
Thu, 12/17/2020 - 23:53
Permalink
check
Thanks for sharing such a valuable information. also you can <a href="http://donstrosin.collectblogs.com/41539569/7-zip-download">check</a>
Libra Charlie
Fri, 12/18/2020 - 01:24
Permalink
My Blog
Hello! I read your article frequently and it's awesome and helpful. My Blog here so You can Check.
http://skindex.manifo.com/
Joser56
Fri, 12/18/2020 - 02:53
Permalink
Click here
I was surfing net and fortunately came across this site and found very interesting stuff here. Its really fun to read. I enjoyed a lot. Thanks for sharing this wonderful information. Click Here:- https://bit.ly/3r8NMqE
ledongrensz
Fri, 12/25/2020 - 00:11
Permalink
Leedon Green is an odyssey of
Leedon Green is an odyssey of curated open spaces and arranging that offer regular quality. It looks to boost outside space to establish https://www.leedon-greens.sg/ really elevating climate. Lavish with trees and vegetation, this is a perfectly created asylum of undulating finished porches wherein lie pockets of recreational spaces and themed pavilions.
ledongrensz
Fri, 12/25/2020 - 00:15
Permalink
Here's a brisk audit on
Here's a brisk audit on Leedon Green and how you would apply land contributing exploration, standards and figurings prior to purchasing a https://www.leedon-greens.sg/ unit in Leedon Green for homestay and additionally land venture purposes.The unit we will dissect will be a 2 room Unit, ideally mid-floor (fourth – eighth floor) and furthermore be contrasting it and Leedon Residence which is 450m away.
acl repair in Delhi
Fri, 12/25/2020 - 22:47
Permalink
Anterior Cruciate Ligament
Anterior Cruciate Ligament which is commonly referred to as ACL is one of the major Ligament in our Knee. http://drashwanimaichand.com/acl-surgery/
ACL can be damaged in part or completely because of diverse sort of sports activities damage. ACL Damage is especially ACL tear prompted all through contact sports activities injury consisting of Basketball, Football, Soccer, Tennis, Downhill, Volleyball, Gymnastics & Rugby. ACL Injury happens particularly in younger generation who's very energetic their day to day life.
Cloe67
Wed, 12/30/2020 - 03:07
Permalink
Visit Here
https://bit.ly/3nz4JYV
Thank you for sharing this informative article. I also have written an article if you want to read it Click Here
Mark76
Wed, 12/30/2020 - 04:37
Permalink
Visit my blog
https://bit.ly/2K9JnTR
I have read your blog. This is really great. I like your work. I also have written an article if you want to read Click Here
Laptops
Mon, 01/04/2021 - 01:35
Permalink
Laptops
Changing a laptop screen may be a daunting assignment for some. However, it isn’t as difficult because it seems to be. In truth, https://homegadgetguides.com/ you may without difficulty replace it at home with out the use of any fancy equipment or professional help.
georgerory
Tue, 01/05/2021 - 21:06
Permalink
Helps brand mindfulness –
Helps brand mindfulness – AdWords make individuals mindful of you as a brand. Your image is promoted when individuals look for utilizing watchwords. https://www.microteklearning.com/vendor/microsoft/ Since they are search advertisements, it advances to individuals in a superior way.
Mark78
Fri, 01/08/2021 - 04:59
Permalink
Check My Blog
This is a really helpful post, very informative there is no doubt about it. Thanks for sharing this information with us. I really appreciate your work. for more information click here https://bit.ly/3s8AIlq
Stefan
Sun, 01/10/2021 - 03:00
Permalink
read it here
you will love to read my first blog which is all bout to pc. https://telechargerpourpc.fr/
mazakin87
Fri, 01/15/2021 - 05:54
Permalink
Click Here
I always spent my half an hour to read this website’s articles or reviews all the time along with a cup of coffee. If you want more interesting Article click here.
https://bit.ly/38L4AwP
Add new comment