js 显示局部图片(一部分)问题

2025-02-25 03:57:55
推荐回答(1个)
回答1:

var t = document.getElementById('img2'),
    s = document.getElementById('img1').getAttribute('src');
//设定div展现区域的大小
t.style.width = '60px'; t.style.height = '60px';
//设定div背景图为你要的图片
t.style.backgroundImage = 'url('+s+')';
//设定背景图的坐标偏移量
t.style.backgroundPosition = '-10px -60px';

这是一个简单的思路,用图片当作背景图,然后通过js控制背景图的css样式来控制偏移量(即你需要显示图片的起始坐标)