2011-07-24 102 views
4

如何使用jQuery或其他JavaScript技术显示图像的一部分?显示部分图像

例子:

image: 
[    ] 
[  -------- ] 
[  -  - ] 
[  - part - ] 
[  -  - ] 
[  -------- ] 
[    ] 
+5

你必须详细说明这个问题。 – yoda

+11

你可以使用CSS做到这一点。 – ThiefMaster

+0

同意ThiefMaster,创建一个具有指定宽度和高度的DIV,并将图像设置为具有CSS中给定背景位置的背景图像 – rabudde

回答

12

操纵CSS属性background,像this

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

这里是如何.animate可见部分:http://jsfiddle.net/wGpk9/2/

4

您可以使用固定大小的一个div并将图像完全放置在里面。然后,您可以使用javascript更改图像的顶部/左侧/右侧/底部位置以将其移动。

<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> 

编辑:那是,如果你想搬过来时的图像......否则,简单地使用CSS

0

,如果你添加样式溢出伊万的示例工作:隐藏,像这样的div ..

<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>