最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Show part of an image - Stack Overflow

programmeradmin1浏览0评论

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

Example:

image:
[                ]
[     --------   ]
[     -      -   ]
[     - part -   ]
[     -      -   ]
[     --------   ]
[                ]

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

Example:

image:
[                ]
[     --------   ]
[     -      -   ]
[     - part -   ]
[     -      -   ]
[     --------   ]
[                ]
Share Improve this question edited Oct 27, 2013 at 5:53 Jonathan Leffler 754k145 gold badges946 silver badges1.3k bronze badges asked Jul 24, 2011 at 17:44 Marcos Roriz JuniorMarcos Roriz Junior 4,10611 gold badges53 silver badges76 bronze badges 3
  • 5 you'll have to elaborate the question. – yoda Commented Jul 24, 2011 at 17:45
  • 11 You can do that using CSS. – ThiefMaster Commented Jul 24, 2011 at 17:45
  • Agree to ThiefMaster, create a DIV with specified width and height and set image as background image with given background-position in CSS – rabudde Commented Jul 24, 2011 at 17:46
Add a comment  | 

3 Answers 3

Reset to default 14

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/

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

Ivan's example works if you add style overflow:hidden to the div like so..

<div style="width: 100px; height: 50px; position: relative; overflow: hidden;"> 
  <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>
发布评论

评论列表(0)

  1. 暂无评论