Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show part of an image

How can I show a part of the image using jQuery or another JavaScript technique?

Example:

image:
[                ]
[     --------   ]
[     -      -   ]
[     - part -   ]
[     -      -   ]
[     --------   ]
[                ]
like image 336
Marcos Roriz Junior Avatar asked Dec 03 '22 07:12

Marcos Roriz Junior


2 Answers

Manipulate the CSS property background, like this:

#imgDiv {
    background-image: url(image.jpg);
    background-position: 10px 50px; /* Visible coordinates in image */
    height: 200px; /* Visible height */
    width: 200px; /* Visible width */
}

Here's how to .animate the visible part: http://jsfiddle.net/wGpk9/2/

like image 173
Simeon Avatar answered Dec 05 '22 20:12

Simeon


You can use a div with fixed size and place the image absolutely positioned inside. You can then use javascript to change the top / left / right / bottom position of the image to move it.

<div style="width: 100px; height: 50px; position: relative"> 
  <img src="path" alt="something" id="image"  
      style="position: absolute; top: 0px; left: 0px" />
</div>

...
<script type="text/javascript">
function moveImage() {
   document.getElementById('image').style.top = '-100px';
}
</script>

EDIT: that's if you want to move the image over time... Otherwise simply use CSS

like image 45
Ivan Nikolchov Avatar answered Dec 05 '22 20:12

Ivan Nikolchov