欢迎来到【一个大西瓜】的博客

不曾为梦想奋斗,拿什么去燃烧青春。有梦之人亦终将老去,但少年心气如昨。
太阳每一个时刻都同时是夕阳和朝阳,每天她沉入西边,意味着她同时从另一面土地升起。
扩大
缩小

jQueryh插件imgareaselect

1、插件介绍

imgareaselect 是一个 允许用户使用简单、直观的点击、拖动界面图像选择矩形区域的jQuery插件。该插件可用于 web 应用程序中轻松实现图像裁剪功能,以及其他功能,如照片标记、 图像编辑功能,等等。
2、基本用法(简单实例)

首先先调用imgareaselect-default.css、jquery-1.7.1.min.js、imgareaselect.pack.js这三个文件(官网下载的)

官网:http://odyniec.net/projects/imgareaselect/

<!--html-->
<
div class="frame" style="margin: 0 0.3em; width: 500px; height: 467px;float:left;border:2px solid #dcdcdc;">   <img id="photo" src="../Images/img3.jpg" style="max-width:100%;max-height:100%;" /> </div> <div id="preview" style="width: 250px; height: 200px; overflow: hidden;border:2px solid #dcdcdc;">   <img src="../Images/img3.jpg" style="width: 250px; height: 200px;" /> </div>

 

function preview(img, selection) {
        if (!selection.width || !selection.height)
            return;

        var scaleX = 250 / selection.width;
        var scaleY = 200 / selection.height;
        var hei = $('#photo').height();
        var wid = $('#photo').width();
        $('#preview img').css({
            width: Math.round(scaleX * wid),
            height: Math.round(scaleY * hei),
            marginLeft: -Math.round(scaleX * selection.x1),
            marginTop: -Math.round(scaleY * selection.y1)
        });
    }

    $(function () {
        $('#photo').imgAreaSelect({
            //aspectRatio: '1:1', 
            handles: true,
            fadeSpeed: 200, onSelectChange: preview
        });
    });

如果在jQuery对象里有不止有一个img元素,那么这个插件会对里面的元素逐个应用此方法。其实这个方法不仅仅应用于img元素,它支持任何块级元素(比如有图像背景的div元素)

实例效果图:

3、实例解析

效果图是通过onSelectChange()回调函数实现选择区域预览的效果。由于预览窗口是250x200px的,因此当截图窗口小于250px时,预览图会放大;当截图窗口大于250px时,预览图会缩小。

在preview()函数中,首先就定义了scaleX与scaleY,它们的内容就是250/selection.width(或height) ,也就是当selection.width小于250时,这个因子起放大作用,反之起缩小作用。

特别值得注意:回调函数中实际图的宽高(要根据实际情况调整!),回调函数中新图的宽高这些参数必须设置正确、否则会出现 选择偏差。(因为我自己的宽高是不定的,所以我是获取的)

 

 
 
 

posted on 2016-12-13 20:22  一个大西瓜咚咚咚  阅读(719)  评论(0编辑  收藏  举报

导航