Jquery:缩放后的图像未正确显示
Jquery: Zoomed image not show correctly
我有以下插件代码。这是一个图片缩放器,并将图像宽度设置为100%;高度为320px。他们已经将这些值作为参数传递。宽度的默认值为320像素。然后我将此宽度转换为100%。问题是,在将宽度转换为百分比之后(因为我是在响应环境中构建的),1。缩放后的图像没有以其全宽显示。当我把光标悬停在图像上时,我必须移动到图像的最左边才能看到预览。如能在这方面提供任何帮助,我们将不胜感激。这是代码片段。
;(function($){
$.fn.picZoomer = function(options){
var opts = $.extend({}, $.fn.picZoomer.defaults, options),
$this = this,
$picBD = $('<div class="picZoomer-pic-wp"></div>').css({'width':opts.picWidth+'%', 'height':opts.picHeight+'px'}).appendTo($this),
$pic = $this.children('img').addClass('picZoomer-pic').appendTo($picBD),
$cursor = $('<div class="picZoomer-cursor"><i class="f-is picZoomCursor-ico"></i></div>').appendTo($picBD),
cursorSizeHalf = {w:$cursor.width()/2 ,h:$cursor.height()/2},
$zoomWP = $('<div class="picZoomer-zoom-wp"><img src="" alt="" class="picZoomer-zoom-pic"></div>').appendTo($this),
$zoomPic = $zoomWP.find('.picZoomer-zoom-pic'),
picBDOffset = {x:$picBD.offset().left,y:$picBD.offset().top};
opts.zoomWidth = opts.zoomWidth||opts.picWidth;
opts.zoomHeight = opts.zoomHeight||opts.picHeight;
var zoomWPSizeHalf = {w:opts.zoomWidth/2 ,h:opts.zoomHeight/2};
$zoomWP.css({'width':opts.zoomWidth+'px', 'height':opts.zoomHeight+'px'});
$zoomWP.css(opts.zoomerPosition || {top: 0, left: opts.picWidth+30+'px'});
$zoomPic.css({'width':opts.picWidth*opts.scale+'px', 'height':opts.picHeight*opts.scale+'px'});
$picBD.on('mouseenter',function(event){
$cursor.show();
$zoomWP.show();
$zoomPic.attr('src',$pic.attr('src'))
}).on('mouseleave',function(event){
$cursor.hide();
$zoomWP.hide();
}).on('mousemove', function(event){
var x = event.pageX-picBDOffset.x,
y = event.pageY-picBDOffset.y;
$cursor.css({'left':x-cursorSizeHalf.w+'px', 'top':y-cursorSizeHalf.h+'px'});
$zoomPic.css({'left':-(x*opts.scale-zoomWPSizeHalf.w)+'px', 'top':-(y*opts.scale-zoomWPSizeHalf.h)+'px'});
});
return $this;
};
$.fn.picZoomer.defaults = {
picWidth: 100,
picHeight: 320,
scale: 2.5,
zoomerPosition: {top: '0', left: '350px'}
/*,
zoomWidth: 320,
zoomHeight: 320*/
};
})(jQuery);
[enter image description here][1]
我试着玩这个插件,我希望这是你想要的结果。
我得到了原始插件,只更改了"缩放"answers"缩放位置"的默认值,但这对更改并不重要。
$.fn.picZoomer.defaults = {
picWidth: 320,
picHeight: 320,
scale: 5, //<--- THIS
zoomerPosition: {top: '350px', left: '0px'} //<--- THIS
/*,
zoomWidth: 320,
zoomHeight: 320*/
};
基本概念是将正确的像素宽度传递给插件。使用responsible,我选择了通过Jquery调整大小,因为插件强制使用像素尺寸,缩放坐标的百分比会出现问题。
$(document).ready(function(){
$('.picZoomer').picZoomer({
picWidth: $(this).width()
});
});
var clrTimeout;
$(window).resize(function(){
clearTimeout(clrTimeout);
clrTimeout = setTimeout(function(){
$('.picZoomer').html($('.imgToSave'));
$('.picZoomer').picZoomer({
picWidth: $(this).width()
});
}, 200);
});
超时对于避免重载调整大小调用非常有用。剩下的最后一个问题是纵横比。
这是JSFiddle示例
希望这对你有所帮助。
相关文章:
- 使用JSP从服务器检索和显示图像
- Windows8应用程序(html&Javascript):从图片库(除了文件选择器)显示图像的另一种方式
- 使用FormData上传AJAX图像;t在服务器端显示图像
- 选择文件后显示图像
- Chrome(webkit?)无法在幻灯片中正确显示图像
- 如何使用javascript显示图像
- 使用JS按顺序显示图像,而不是随机显示
- 使用服务器上的HTML/js在网页上显示图像
- 我想在选择输入文件上显示图像
- Javascript:单击单选按钮时显示图像
- 如何将JSON结果设置为'src'共'img'以在ASP.NET MVC4中显示图像
- 无法在cakepp布局中显示图像
- 页面加载后通过javascript显示图像
- 将图像文件存储在猫鼬模式中的二进制数据中,并以html形式显示图像
- 使用AngularJs数据绑定的三元运算符显示图像
- 加载时画布上未显示图像
- 运行POST时显示图像
- NodeJS上没有显示图像
- 从Javascript在表中显示图像
- 如何在MVC5中运行时在HTML5 Canvas中显示图像