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

Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You are doing a great job Man. keep it up. Here am also tried something. You may check it.
https://bit.ly/3cp1Ln3

Привет друзья!
Предлагаем Вашему вниманию изделия из стекла для дома и офиса. Наша организация работает 10 лет на рынке этой продукции в Беларуси. Изготовим для вас и установим душевую кабину из стекла на заказ. Профессиональные замерщики и монтажники. Мы используем различные типы материалов и фурнитуры для создания душевых кабин и душевых перегородок любого дизайна и конструкции, устанавливаем на поддоны или без поддонов. У нас вы сможете заказать стекло для душевой кабины по индивидуальным размерам и проекту. Выполним доставку изделия и его монтаж.
Более подробная информация размещена https://drive.google.com/file/d/13CAPanRKgpkKCcaHzI0EiaMtWM0saPJO/view?u...

In this male ruling world, a ton of ladies have good thoughts and a little wish to go into business. For some ladies it simply remains as https://www.stuffroots.com Planning to switch your telephone? This is actually an energizing and bright thing. Yet, kindly remember the significant information put away on the old telephone.

Extraordinary compared to other tip I can give you is that you Trust is one of the fundamental business resources. On the off chance that you https://www.stuffroots.com expect to improve the change rates, maybe you should begin by upgrading your image trust. As Steve Forbes says, "Your image is the absolute most significant speculation you can make in your business.

For the single mother, it is very difficult to live single and develop a kid and live happily. Struggling between job and kid make the mother mentally and emotionally stressed.<a href="https://www.trendsnbest.com/free-cars-for-single-moms/">cars for single moms</a>
Just how single mothers help with getting a car on that covered the different important topic for the single mother in need of a vehicle.

This is a great blog. It very reliable for new readers. It really makes me happy. Thanks for sharing it. I am a gamer and here is something interesting for you. You may check this https://bit.ly/3uFha9q

berbagai permainan di web. Kami akan memeriksa lebih banyak ruang online dan hasilnya di artikel. Ikuti artikel untuk mendapatkan http://45.77.39.143/ data poin demi poin. Kami secara keseluruhan menyadari bahwa pembukaan adalah sebuah mesin. Dengan asumsi kami menggambarkan klub perjudian konvensional, mesin permainan ditemukan dengan hati dan lebih dari tiga atau empat gulungan bergerak lurus. Gulungan

This is extremely helpful info!! for gamers Very good work. Everything is very interesting to learn and easy to understand. Take the given information. Hi, my name is Johny dekar, and I am gaming smith. I play a game called Minecraft. and having site about how to edit a Minecraft skin https://erickaweber60.doodlekit.com/blog/entry/10978554/how-to-edit-a-mi...

I'm glad I found this web site, I couldn't find any knowledge on this matter prior to. Also operate a site and if you are ever interested in doing some visitor writing for me if possible feel free to let me know, i am always look for people to check out my web site. https://www.blurb.com/user/Rstarck?profile_preview=true white page

I have just gone through your website and I am highly convinced that you can offer quality <a href="https://www.economicshomeworkhelper.com/">economics homework help</a>. I have seen several
samples and the quality looks good. If the samples are done by you then I am more than willing to hire you for my assignment. I just hope that anytime I contact you for an<a href="https://www.economicshomeworkhelper.com/">economics homework help</a> you will be available to help because some of these assignments are always very urgent.

I have a ledger posting assignment involving several entries. I am currently fishing for the best <a href="https://www.accountingassignmenthelper.com/">Accounting Homework Help</a> tutor to help me with the task and from what I have read, you guys don’t seem too bad at your job except for the few flaws. I may consider taking <a href="https://www.accountingassignmenthelper.com/tutors/">Accounting Assignment Help</a> from here and hopefully I will be back with positive feedback.

I am happy that many students have received the right <a href="https://www.mathsassignmenthelp.com/experts/">Math assignment help</a> through you. I am in my third year in college and I have not had a chance to get such help. Since I have come across your post, I hope I will find the right <a href="https://www.mathsassignmenthelp.com/">Math homework help</a> through you. My only obstacle would be finances. I hope that you are pocket friendly or should I call it student-friendly

This is very great and helpful information. This is very helpful for newcomers. Thanks for sharing this great article. I am a gamer and I would like to suggest to you an auto-clicker which is really interesting. You may visit here https://hardleynenon.wixsite.com/gamerspoint/post/5tipdantrikyangakanmem...

Pages

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.