0

There are two images placed one below the other, what i want to do is when you hover a mouse over the top image only the portion of the image below should be visible not the entire image to be replaced.Is this possible using jquery ?.

enter image description here

I am stuck on where to start. I tried changing the div background on hover but i couldn't get anywhere near what i need.Thanks.

html

<div style="background-image: url("image1.jpg")></div>
<div style="background-image: url("image2.jpg")></div>
9
  • 2
    Can you paste your html part here.. Commented Apr 18, 2016 at 18:11
  • Its just two divs with background images positioned one top the other using absolute positioning Commented Apr 18, 2016 at 18:12
  • 1
    no one is going to be able to help you if you dont post the code you are using. Commented Apr 18, 2016 at 18:19
  • sorry for not adding the html code as I said it only consist of two divs with position and the javascript code which i worked on is totally irrelevant to my problem hence i didn't add it in. I didn't know how to proceed further. Commented Apr 18, 2016 at 18:31
  • Think your approach to solving is wrong. Why 2 images? Why only radial Image is visible? What should be achieved exactly? Commented Apr 18, 2016 at 18:31

3 Answers 3

2

Try this?

$(document).ready(function() {
  var $hover = $("#hover");
  var $foreground = $("#foreground");

  $hover.hide();

  $foreground.mousemove(function(event) {
    var top = event.pageY - $hover.height() / 2;
    var left = event.pageX - $hover.width() / 2;

    $hover.css("top", top);
    $hover.css("left", left);
  });

  $foreground.mouseover(function() {
    $hover.show();
  });

  $foreground.mouseleave(function() {
    $hover.hide();
  });
});

http://jsfiddle.net/WV8jX/685/

EDIT: Updated to hide before mouse entering http://jsfiddle.net/WV8jX/686/

EDIT: It doesn't really solve your problem tho if you need the background to be shown as according to your description.

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

4 Comments

Yeah, It doesn't work with images as i asked. Is it even possible to do that kinda effect ?
Hmm... sounds hard to do tho. I was thinking of placing the top layer as an element with very thick border. But it wouldn't work as well as it will be the top not being able to show instead of the background not being able to show.
one more query here, you want same image (some part of bg but fixed) to be moved along with cursor or according to the cursor position that part of the background image to be shown.?
It as to be according to the cursor position the part of the image to be shown
0

Since you have tagged jQuery, use the jQuery .hover().

Example:

$("selector").hover(
  function() {
    show your hidden image functionality here
  }
);

https://api.jquery.com/hover/

Comments

0

I think there is no need of using Javascript/Jquery. CSS can do the job as below

HTML

<div class="bgdemo"></div>

CSS

.bgdemo{
     background-image: url("image1.jpg");
}

.bgdemo:hover{
         background-image: url("image2.jpg");
    }

and there is a typo in your HTML, style tag is not ending properly.

edit

Check this jsfiddle, Position the background according to your requirement.

edit

Here I found good article seems bit complicated but worth reading..

2 Comments

I don't want the entire image to replaced on hover. I only want the radial section of the bottom part to be visible on hover
I am sorry if i wasn't clear enough, what i want is the circle to move along with the cursor. Is it possible to dynamically clip the object along with the mouse cursor

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.