回答:
唉,JavaScript 根本无法提取执行此类操作所需的图像属性。但是,HTML 元素与一些服务器端脚本相结合的形式可能会有所帮助。
...
< ? (open php)
$large_image = 'path/to/large_image';
$full_w = imagesx($large_image);
$full_h = imagesy($large_image);
(close php) ? >
这可以在 Javascript 中完成,只需 google 一下:
var newimage = new Image();
newimage.src = document.getElementById('background').src;
var height = newimage.height;
var width = newimage.width;
这会从现有图像生成新图像,并以这种方式在 java 脚本中捕获原始图像的原始高度和宽度属性(而不是作为背景的图像。
回答:
文档图像对象的宽度/高度属性是只读的。然而,如果你可以改变它们,你只会挤压框架,而不是像你想要的那样切割框架。您想要的那种图像处理无法使用客户端 javascript 完成。我建议在服务器上剪切图像,或者在图像上覆盖一个 div 以隐藏您不希望显示的部分。
...
var newimage = new Image();
newimage.src = document.getElementById('background').src;
var height = newimage.height;
var width = newimage.width;
newimage.style.height = '200px';
newimage.style.width = '200px';
newimage.height = '200px';
newimage.width = '200px';
如果需要的话:
newimage.setAttribute('height','200px');
在某些情况下需要加倍的 newimage.style.height 和 newimage.height 以确保 IE 及时了解图像已调整大小(您将在之后立即渲染该内容,而内部IE 处理太慢了。)
感谢上面的脚本,我在http://morethanvoice.net/m1/reader13.php(右键菜单...鼠标悬停放大)上进行了修改和实现,即使在 IE 中也是正确的,但是您会注意到,对于旧样式的 IE,on mousemove 图像处理速度太快了, 渲染位置,但只渲染一次图像。无论如何,欢迎任何好主意。
感谢大家的关注,希望以上代码可以帮助到大家...
克劳迪奥·克伦普
http://morethanvoice.net/m1/reader13.php