3

I am creating a hover effect using jQuery and data-attribute.

So what I did is that I put some data attribute on each images and then I access them via jQuery and store them in a variable. Then I access them to change the image via jQuery. However, I have no idea how to put it back to the original on "mouseout"

Here's my jQuery:

$(document).ready(function(){
    $('img').hover(function(){
        var s = $(this).attr('data-alt-src');
        $(this).attr('src', s);
    }, function(){
        $(this).attr('src');
    });
});

Any idea?

0

5 Answers 5

2

try to set origional src to other attribute and use that when mouseleave.

 $(document).ready(function(){
    $('img').hover(function(){
        var s = $(this).attr('data-alt-src');            
        $(this).attr('orSrc',$(this).attr('src')).attr('src', s);
    }, function(){
        $(this).attr('src',$(this).attr('orSrc'));
    });
});

Demo

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

4 Comments

Can I ask a question? Why can't we declare the var old inside the $(document).ready(function(){} ??? ABOVE THE var old src. ??? – Kimberly Wright
Can you explain further why?
what will happen if I will declare these whole thing above the hover function: var original_src=$(this).attr('src'); would it still be accessible on mouse out?
not at all because $(this) refers to hoved image(i.e which image being hoved) so it will throw an error
1

To store current src as a variable and use it

   var old="";
$(document).ready(function(){
        $('img').hover(function(){
            old=$(this).attr('src');
            var s = $(this).attr('data-alt-src');
            $(this).attr('src', s);
        }, function(){
            $(this).attr('src',old);
        });
    });

Updated Fiddle

1 Comment

Can I ask a question? Why can't we declare the var old inside the $(document).ready(function(){} ???
1

    $(document).ready(function() {
      var src_original;
      $('img').hover(function() {
        src_original = $(this).attr('src');
        var s = $(this).attr('data-alt-src');
        $(this).attr('src', s);
      }, function() {
        $(this).attr('src', src_original);
      });
    });
img {
  cursor: pointer;
}
img:hover {
  opacity: 0.7;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
  <img src="http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/1.jpg" data-alt-src='http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/1-1.jpg' />
  <img src="http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/4.jpg" data-alt-src='http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/2-2.jpg' />
  <img src="http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/3.jpg" data-alt-src='http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/3-3.jpg' />
  <img src="http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/2.jpg" data-alt-src='http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/4-4.jpg' />

</section>

3 Comments

Can I ask a question? Why can't we declare the var old inside the $(document).ready(function(){} ??? ABOVE THE var old src. ???
@KimberlyWright It is declared in inside DOM ready, but if you are asking about why not inside hover function it won't be available for mouseout function, since it is a local variable.
so we need to declare it as an empty variable outside the hover function first and then set a value on it inside the hover function am I correct?
0

Store original image src in cache using .data().

$(document).ready(function () {
    $('img').hover(function () {
        var that = $(this);
        that.data('or-src', that.prop('src'))
        that.prop('src', that.data('alt-src'));
    }, function () {
        var that = $(this);
        $(this).prop('src', that.data('or-src'));
    });
});

DEMO

Comments

0

I added data-old-src to store default img source and load on mouse out.

Check https://jsfiddle.net/jzxh1asx/6/

HTML

  <section>
    <img src="http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/1.jpg" data-old-src="http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/1.jpg" data-alt-src='http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/1-1.jpg' />
    <img src="http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/4.jpg" data-old-src="http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/4.jpg" data-alt-src='http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/2-2.jpg' />
    <img src="http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/3.jpg" data-old-src="http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/3.jpg" data-alt-src='http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/3-3.jpg' />
    <img src="http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/2.jpg" data-old-src="http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/2.jpg" data-alt-src='http://halloweenmaternitycostumes.com/wp-content/uploads/2015/06/4-4.jpg' />
  </section>

JS

$(document).ready(function(){
    $('img').hover(function(){
        var s = $(this).attr('data-alt-src');
        $(this).attr('src', s);
    }, function(){
        var os = $(this).attr('data-old-src');
        $(this).attr('src', os);
    });
});

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.