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
3. Configure Rollover image field
 

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
4. Configure Regular image field
4. Configure Regular image field

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 :

rollover.js file location

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 :

zeropoint.info

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

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 :)

Hi Phinkalily
Could you share your CSS code with us?
Many thnaks

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?

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???

javascript for rollover is working in other browsers but, for IE8 & IE9 it is not working. Can you suggest any solution?

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]"/>

I want to give a link to image, when clicking particular image then redirect to that particular content. How to do that ?

As the blocky exploration game and creativity tool reaches its first decade, our games writer – and the father of someone on the autism spectrum – reflects on the impact it has had https://sandys-guide-21.webself.net

As an eatery in web based life, I would go for collaborations with way of life and nourishment bloggers. They starve for new nourishment around the local area and great substance for their blog, video blog or digital recording. As a matter of first importance, you have to distinguish them. Once more, online life checking can be of assistance here. It's really straightforward – screen watchwords identified with your business zone, for example eatery, audit, nourishment, blog. Thusly, an online life checking instrument will indicate you creators and sources (web journals, blog entries) that notice catchphrases pertinent to what you do.
click here: https://www.papayapr.com.au/work/

Organizations today are going to versatile applications to grow their techniques to tap a higher client base. With over 86.8% offer, Android OS commands the portable application improvement showcase. Utilizing the advantages of Android application improvement is, in this manner, a need for associations. It is relied upon to flood considerably promote in the coming years.

https://twitter.com/JustinGates95

These shorter seasons additionally have been alluded to as ".5" or half seasons, where the run of shows https://pinoybaytv.su/ among September and December is named "Season X", and the second run among January and May named "Season X.5". Instances of this incorporate the 2004 manifestation of Battlestar Galactica, ABC's FlashForward, and ABC Family's Make It or Break It.

I will take a note of your site and continue checking for new data about once every week.You know, many persons are looking round for this information, https://fxprofitpips.com/ you could help them greatly.Keep sharing your thoughts.

Also, they will get the updates and information related to the company. They can also connect with the company and can easily manage their paystub information.
https://about.me/amitpareek

. So feel free to change your telephone into a versatile opening machine or a convenient baccarat table. The gambling club online application is accessible for both Android and Apple gadgets, making it simpler to appreciate the rush of portable club games anyplace you are.
to visit her: https://link-exchange.name

The gambling club online application is accessible for both Android and Apple gadgets, making it simpler to appreciate the rush of portable club games anyplace you are.
https://shareits.xyz/

Thank you so much for the information. Very good post https://apkmoto.com/

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.