我正在尝试制作一个剪切窗口,但它没有发生,这里是代码示例:
<html>
<head>
<style>
#clippingWindow
{
height:178px;
width:278px;
overflow: hidden;
}
#bgImg
{
left:35px;
border:54px;
}
</style>
<script language="javascript">
function getImgSize()
{
var ds = document.getElementsByName('a');
var wid = ds[0].width;
var pos = (wid-278)/2;
alert(pos);
alert(ds[0].id);
if(ds[0].width>278)
{
dt.style.left=500; //this is not working
}
}
</script>
</head>
<body><input type="button" onClick="getImgSize();">
<div id="clippingWindow">
<img name="a" src="sss.bmp" id="bgImg">
</div><br><br><br>
<div id="ad">
<img name="a" src="484.jpeg">
</div>
</body>
</html>
最佳答案
您可能需要将要定位的元素的 position
属性设置为 absolute
或 relative
(例如 ds.style.position =“相对”;
)。 left
属性对静态定位的元素没有任何影响。
此外,您可能需要使用“500px”
而不是数字500
。
关于javascript - 使用javascript定位div的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12590882/