【问题标题】:Resizing to fit height jquery.panzoom调整大小以适应高度 jquery.panzoom
【发布时间】:2014-09-23 20:17:39
【问题描述】:

我正在尝试在初始化 jquery.panzoom 时如何制作比浏览器窗口 (2000x2800px) 比例更大的图像。我需要图像自行调整大小,使其适合容器的高度。

查看这个 jsFiddle:http://jsbin.com/raxejirubayu/1/edit?html,css,js,output

有任何使用 panzoom 经验的人可以帮助我吗?作为回报,提供虚拟啤酒和欢呼。

【问题讨论】:

    标签: javascript jquery resize zooming pan


    【解决方案1】:

    我已经通过编写这段 javascript 代码解决了这个问题,它基本上根据图像大小和容器大小计算缩放和平移参数,并应用转换使图像被拉伸到容器边界。

    Javascript 文件

    $(document).ready(function(){
        $panzoom = $(".cimage").panzoom();
    
        //Wait for the image to load
        $('.cimage').load(function(){
            //Get container and image
            var image = $('.cimage');
            var container = $('.image-container');
    
            //Get container dimensions
            var container_height = container.height();
            var container_width = container.width();
    
            //Get image dimensions
            var image_height = image.height();
            var image_width = image.width();
    
            //Calculate the center of image since origin is at x:50% y:50%
            var image_center_left = image_width / 2.0;
            var image_center_top = image_height / 2.0;
    
            //Calculate scaling factor
            var zoom_factor;
    
            //Check to determine whether to stretch along width or heigh
            if(image_height > image_width)
                zoom_factor = container_height / image_height;
            else
                zoom_factor = container_width / image_width;
    
            //Zoom by zoom_factor
            $panzoom.panzoom("zoom", zoom_factor, {animate: false});
    
            //Calculate new image dimensions after zoom
            image_width = image_width * zoom_factor;
            image_height = image_height * zoom_factor;
    
            //Calculate offset of the image after zoom
            var image_offset_left = image_center_left - (image_width / 2.0);
            var image_offset_top = image_center_top - (image_height / 2.0);
    
            //Calculate desired offset for image
            var new_offset_left = (container_width - image_width) / 2.0;
            var new_offset_top = (container_height - image_height) / 2.0;
    
            //Pan to set desired offset for image
            var pan_left = new_offset_left - image_offset_left;
            var pan_top = new_offset_top - image_offset_top;
            $panzoom.panzoom("pan", pan_left, pan_top);
    
        });
    });
    

    HTML 文件

    <div class="image-container">
        <img class='cimage' src="abc.jpg'/>
    </div>
    

    它会沿宽度或高度拉伸,具体取决于图像方向(纵向或横向),但如果您只想沿高度拉伸,则需要替换

    if(image_height > image_width)
        zoom_factor = container_height / image_height;
    else
        zoom_factor = container_width / image_width;
    

    zoom_factor = container_height / image_height;
    

    但我建议不要这样做,因为它无法处理横向图像。

    【讨论】:

    • 这确实提供了混合结果与相对较大的图像,以及移动设备。似乎泛计算需要一点调整
    • 我认为它会起作用,因为我没有在平移计算中使用原始图像宽度和高度,它们会在图像缩小/缩小后重新计算。
    • 我发现虽然缩放工作正常,但随后的平移似乎没有效果,并且图像位于视口的右侧,而不是在中间。我看不出如何解决这个问题。 stackoverflow.com/questions/32583854/…
    猜你喜欢
    • 2016-02-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    相关资源
    最近更新 更多