admin管理员组

文章数量:1392073

I want to use parallax effect with only 1 image. Can it be done?

Actually, it's not really parallax, I have one big image in the center of screen, so when I move my mouse to the left, I want the image slightly moves to the right, move mouse to the right, the image slightly moves to the left, move mouse up image moves down, move mouse down image moves up.

I think I've seen this effect before, but I kinda forget where I see it. Really appreciate help. Thanks

I want to use parallax effect with only 1 image. Can it be done?

Actually, it's not really parallax, I have one big image in the center of screen, so when I move my mouse to the left, I want the image slightly moves to the right, move mouse to the right, the image slightly moves to the left, move mouse up image moves down, move mouse down image moves up.

I think I've seen this effect before, but I kinda forget where I see it. Really appreciate help. Thanks

Share Improve this question asked Feb 8, 2011 at 5:51 HensonHenson 5,94312 gold badges48 silver badges61 bronze badges 2
  • 2 Just for some inspiration/reference, github has an awesome example: github./404 – Christian Commented Feb 8, 2011 at 6:34
  • Technically, it's impossible to get parallax with just one, 2-D image. You can shift the whole image, like DhruvPathak's answer, but that's not parallax. Parallax requires multiple images like the github page. Or, you could use CSS sprites or slicing techniques to embed multiple parts in one image. – Brock Adams Commented Feb 8, 2011 at 9:01
Add a ment  | 

5 Answers 5

Reset to default 3

Here is how it can be done, you can tweak and improve it. Not tested on all browsers,works good on firefox. Cheers!

See it in action at jsFiddle.

<html>
<head>
<script>
document.onmousemove = shiftImageXY;

var tempX = 0;
var tempY = 0;
var oldTempX = 0;
var oldTempY = 0;
var IE =  !!(window.attachEvent&&!window.opera);

function shiftImageXY(e) {
  if (IE) { 
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY + document.body.scrollTop;
  } else {  
    tempX = e.pageX;
    tempY = e.pageY;
  }  
  img = document.getElementById('myImage');
  speedFactorDamping = 0.1; // change this for faster movement
  xdir = (tempX - oldTempX) ;
  ydir = (tempY - oldTempY) ;
  parallexX = -xdir*speedFactorDamping;
  parallexY = -ydir*speedFactorDamping;
  currX = parseInt(img.offsetLeft);
  currY = parseInt(img.offsetTop);

  img.style.left = (currX + parallexX) + 'px';
  img.style.top = (currY + parallexY) + 'px';
  oldTempX = tempX;
  oldTempY = tempY;
  return true;
}

</script>

</head>
<body>
<div style='height:300px;clear:both;text-align:center;'></div>
<div style='height:800px;width:800px;text-align:center;'>
<img id='myImage' src='http://img516.imageshack.us/img516/7355/icon.png' style='position:absolute' />
</div>

</body>

Here is the exact plugin that github uses for it's parallax effect:

https://github./cameronmcefee/plax

From the documentation:

$('#plax-octocat').plaxify({"xRange":40,"yRange":40});
$('#plax-earth').plaxify({"xRange":20,"yRange":20,"invert":true});
$('#plax-bg').plaxify({"xRange":10,"yRange":10,"invert":true});
$.plax.enable();

Just set the invert:true variables for items behind and set higher values for stuff in front of the focus item and you're set.

I hope you mean an effect like this - bouncing orbs

If yes, you can do it using one image only. You draw it such that, If your image is like

----------
|image   |
|--------|


then you draw it in two parts

-------
age   |im
------|
      ^
      |
      this being the start of the second draw.

This would give you the parallax effect.

Coding it should be easy, just changing the reference point of drawing rectangles of a image using a modulus of width.

There are some jQuery plugins that do some of the things you mention.

Image parallax can create a depth-like parallax using a single image or collection of images

http://plugins.jquery./project/imageparallax

But I think maybe Background Parallax is what you actually want - so the background moves at a different rate to the scroll, like on Android home screens...

http://plugins.jquery./project/backgroundparallax

A small link is always better than a long discussion ;)

Check out github 404 page!

本文标签: javascriptparallax with 1 imageStack Overflow