1

I don't want the images to load until the link is hovered, so for that here is the script but it's not working, can someone help me in correcting it? I added an event listener to each link, and on mouseover called a function that sets the image. To make things more interesting, we can copy lazyload a bit here and also use the data-original property :)

For example: this is the HTML Code:

<li>
   <a href="#" class="tip_trigger">
      <img class="tip" alt="300 Grams"
              data-original="https://lh5.googleusom/-qCRpw.../300%2520.jpg"
              src='empty.gif' width="90" height="90"/>
      Alex
   </a>
</li>

Then, add an event listener:

Code:

// call for each element with the class 'tip' that's being hovered upon
$('.tip').hover(function()
{
   // retrieve the image inside this element
   var elem = $(this).find('img');

   // set the 'src' to the 'data-original' value
   elem.attr('src', elem.attr('data-original'));
});

for a live demo, you can see this page http://bloghutsbeta.blogspot.com/2012/04/testing-slidernav.html the above part of script is only added to the first letter ALEX in 'A' Category.

7
  • 5
    How is it "not working?" Commented Apr 23, 2012 at 13:14
  • 1
    The live demo nearly crashes my browser because it uses JavaScript for all the effects. Have you ever heard of CSS3 transitions? Commented Apr 23, 2012 at 13:15
  • 1
    @TomvanderWoerdt Maybe you need a better browser/computer. ;) Commented Apr 23, 2012 at 13:19
  • @epascarello: No, that's not it :-) Commented Apr 23, 2012 at 13:19
  • 1
    @TomvanderWoerdt well I think epascarello is not wrong about the browser idea. Commented Apr 23, 2012 at 13:39

4 Answers 4

11

.tip is already the image so you cannot find an image with $(this).find('img');

try instead

$('.tip_trigger').hover(function()
{
   // retrieve the image inside this element
   var elem = $(this).find('img');

   // set the 'src' to the 'data-original' value
   elem.attr('src', elem.data('original'));
});

use also .data() method instead of .attr()

Sign up to request clarification or add additional context in comments.

5 Comments

WOW, you are a genius :) it works, can you tell me like if you are sure about it, that by doing this NO IMAGES WILL load until they are hovered? as I am scared to death if images still load even if it is not hovered
yes, the only way that those images have to be loaded is triggering the event (via javascript). So I strongly suggest you to think a technique to ensure accessibility when javascript is not enabled
Well I am not worried with that as I don't want the visitors to come who don't have javascript :) Now I am really really confused, If this much small script can make life easy WHY AM I USING LAZYLOAD, isn't there a script like this that do the same thing Just instead of hover it WORKS FOR SCROLL.
lazyload on scrolling is a little-bit different technique. :) Usually on scroll you load some images and then you soon show them as soon as they are ready. Here instead you load and wait a single image every time an hover occurs
Oh so it means I am left to lazyLoad :P I was asking because there is one annoying thing that lazyload is doing and I don't know what to do about it. I have done a post about it too but no one replied, If you don't mind can you have a look at it, here it is stackoverflow.com/questions/10235372/…
1

var elem = $(this).find('img'); <-- you are looking for an image when this is the image.

Either replace the element you attach hover to

$('.tip_trigger').hover(function()
{
   // retrieve the image inside this element
   var elem = $(this).find('img');

   // set the 'src' to the 'data-original' value
   elem.attr('src', elem.attr('data-original'));
});

or keep it the way it is and do not look for an image

$('.tip').hover(function()
{
   var elem = $(this);

   // set the 'src' to the 'data-original' value
   elem.attr('src', elem.attr('data-original'));
});

Comments

0

You have bound the hover to the img element, therefor you can't find a img child. Just use this, or change the hover to .tip_trigger.

$('.tip_trigger').hover(function() // That should do the trick
{
   // retrieve the image inside this element
   var elem = $(this).find('img');

   // set the 'src' to the 'data-original' value
   elem.attr('src', elem.attr('data-original'));
});

Comments

0

jQuery.find() searches children. You have already found the img with your ".tip"-selector.

var elem = $(this);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.