您的位置: 首页 > 技术文档 > 网页制作 > JavaScript 图片切割效果
不唐突的JavaScript的七条准则 回到列表 连续字符自动换行的解决方案
 JavaScript 图片切割效果

作者:cloudgamer 时间: 2008-12-08 文档类型:原创 来自:蓝色理想

第 1 页 JavaScript 图片切割效果 [1]
第 2 页 JavaScript 图片切割效果 [2]
第 3 页 JavaScript 图片切割效果 [3]
第 4 页 JavaScript 图片切割效果 [4]

这里贴出切割效果部分的代码

var isIE = (document.all) ? true : false;
var $ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};
var Class = {
    create: function() {
        return function() { this.initialize.apply(this, arguments); }
    }
}
var Extend = function(destination, source) {
    for (var property in source) {
        destination[property] = source[property];
    }
}
var Bind = function(object, fun) {
    return function() {
        return fun.apply(object, arguments);
    }
}
var BindAsEventListener = function(object, fun) {
    var args = Array.prototype.slice.call(arguments).slice(2);
    return function(event) {
        return fun.apply(object, [event || window.event].concat(args));
    }
}
var CurrentStyle = function(element){
    return element.currentStyle || document.defaultView.getComputedStyle(element, null);
}
function addEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.addEventListener) {
        oTarget.addEventListener(sEventType, fnHandler, false);
    } else if (oTarget.attachEvent) {
        oTarget.attachEvent("on" + sEventType, fnHandler);
    } else {
        oTarget["on" + sEventType] = fnHandler;
    }
};
function removeEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else {
        oTarget["on" + sEventType] = null;
    }
};
//图片切割
var ImgCropper = Class.create();
ImgCropper.prototype = {
  //容器对象,控制层,图片地址
  initialize: function(container, handle, url, options) {
    this._Container = $(container);//容器对象
    this._layHandle = $(handle);//控制层
    this.Url = url;//图片地址
   
    this._layBase = this._Container.appendChild(document.createElement("img"));//底层
    this._layCropper = this._Container.appendChild(document.createElement("img"));//切割层
    this._layCropper.onload = Bind(this, this.SetPos);
    //用来设置大小
    this._tempImg = document.createElement("img");
    this._tempImg.onload = Bind(this, this.SetSize);
   
    this.SetOptions(options);
   
    this.Opacity = Math.round(this.options.Opacity);
    this.Color = this.options.Color;
    this.Scale = !!this.options.Scale;
    this.Ratio = Math.max(this.options.Ratio, 0);
    this.Width = Math.round(this.options.Width);
    this.Height = Math.round(this.options.Height);
   
    //设置预览对象
    var oPreview = $(this.options.Preview);//预览对象
    if(oPreview){
        oPreview.style.position = "relative";
        oPreview.style.overflow = "hidden";
        this.viewWidth = Math.round(this.options.viewWidth);
        this.viewHeight = Math.round(this.options.viewHeight);
        //预览图片对象
        this._view = oPreview.appendChild(document.createElement("img"));
        this._view.style.position = "absolute";
        this._view.onload = Bind(this, this.SetPreview);
    }
    //设置拖放
    this._drag = new Drag(this._layHandle, { Limit: true, onMove: Bind(this, this.SetPos), Transparent: true });
    //设置缩放
    this.Resize = !!this.options.Resize;
    if(this.Resize){
        var op = this.options, _resize = new Resize(this._layHandle, { Max: true, onResize: Bind(this, this.SetPos) });
        //设置缩放触发对象
        op.RightDown && (_resize.Set(op.RightDown, "right-down"));
        op.LeftDown && (_resize.Set(op.LeftDown, "left-down"));
        op.RightUp && (_resize.Set(op.RightUp, "right-up"));
        op.LeftUp && (_resize.Set(op.LeftUp, "left-up"));
        op.Right && (_resize.Set(op.Right, "right"));
        op.Left && (_resize.Set(op.Left, "left"));
        op.Down && (_resize.Set(op.Down, "down"));
        op.Up && (_resize.Set(op.Up, "up"));
        //最小范围限制
        this.Min = !!this.options.Min;
        this.minWidth = Math.round(this.options.minWidth);
        this.minHeight = Math.round(this.options.minHeight);
        //设置缩放对象
        this._resize = _resize;
    }
    //设置样式
    this._Container.style.position = "relative";
    this._Container.style.overflow = "hidden";
    this._layHandle.style.zIndex = 200;
    this._layCropper.style.zIndex = 100;
    this._layBase.style.position = this._layCropper.style.position = "absolute";
    this._layBase.style.top = this._layBase.style.left = this._layCropper.style.top = this._layCropper.style.left = 0;//对齐
    //初始化设置
    this.Init();
  },
  //设置默认属性
  SetOptions: function(options) {
    this.options = {//默认值
        Opacity:    50,//透明度(0到100)
        Color:        "",//背景色
        Width:        0,//图片高度
        Height:        0,//图片高度
        //缩放触发对象
        Resize:        false,//是否设置缩放
        Right:        "",//右边缩放对象
        Left:        "",//左边缩放对象
        Up:            "",//上边缩放对象
        Down:        "",//下边缩放对象
        RightDown:    "",//右下缩放对象
        LeftDown:    "",//左下缩放对象
        RightUp:    "",//右上缩放对象
        LeftUp:        "",//左上缩放对象
        Min:        false,//是否最小宽高限制(为true时下面min参数有用)
        minWidth:    50,//最小宽度
        minHeight:    50,//最小高度
        Scale:        false,//是否按比例缩放
        Ratio:        0,//缩放比例(宽/高)
        //预览对象设置
        Preview:    "",//预览对象
        viewWidth:    0,//预览宽度
        viewHeight:    0//预览高度
    };
    Extend(this.options, options || {});
  },
  //初始化对象
  Init: function() {
    //设置背景色
    this.Color && (this._Container.style.backgroundColor = this.Color);
    //设置图片
    this._tempImg.src = this._layBase.src = this._layCropper.src = this.Url;
    //设置透明
    if(isIE){
        this._layBase.style.filter = "alpha(opacity:" + this.Opacity + ")";
    } else {
        this._layBase.style.opacity = this.Opacity / 100;
    }
    //设置预览对象
    this._view && (this._view.src = this.Url);
    //设置缩放
    if(this.Resize){
        with(this._resize){
            Scale = this.Scale; Ratio = this.Ratio; Min = this.Min; minWidth = this.minWidth; minHeight = this.minHeight;
        }
    }
  },
  //设置切割样式
  SetPos: function() {
    var p = this.GetPos();
    //按拖放对象的参数进行切割
    this._layCropper.style.clip = "rect(" + p.Top + "px " + (p.Left + p.Width) + "px " + (p.Top + p.Height) + "px " + p.Left + "px)";
    //设置预览
    this.SetPreview();
  },
  //设置预览效果
  SetPreview: function() {
    if(this._view){
        //预览显示的宽和高
        var p = this.GetPos(), s = this.GetSize(p.Width, p.Height, this.viewWidth, this.viewHeight), scale = s.Height / p.Height;
        //按比例设置参数
        var pHeight = this._layBase.height * scale, pWidth = this._layBase.width * scale, pTop = p.Top * scale, pLeft = p.Left * scale;
        //设置预览对象
        with(this._view.style){
            //设置样式
            width = pWidth + "px"; height = pHeight + "px"; top = - pTop + "px "; left = - pLeft + "px";
            //切割预览图
            clip = "rect(" + pTop + "px " + (pLeft + s.Width) + "px " + (pTop + s.Height) + "px " + pLeft + "px)";
        }
    }
  },
  //设置图片大小
  SetSize: function() {
    var s = this.GetSize(this._tempImg.width, this._tempImg.height, this.Width, this.Height);
    //设置底图和切割图
    this._layBase.style.width = this._layCropper.style.width = s.Width + "px";
    this._layBase.style.height = this._layCropper.style.height = s.Height + "px";
    //设置拖放范围
    this._drag.mxRight = s.Width; this._drag.mxBottom = s.Height;
    //设置缩放范围
    if(this.Resize){ this._resize.mxRight = s.Width; this._resize.mxBottom = s.Height; }
  },
  //获取当前样式
  GetPos: function() {
    with(this._layHandle){
        return { Top: offsetTop, Left: offsetLeft, Width: offsetWidth, Height: offsetHeight }
    }
  },
  //获取尺寸
  GetSize: function(nowWidth, nowHeight, fixWidth, fixHeight) {
    var iWidth = nowWidth, iHeight = nowHeight, scale = iWidth / iHeight;
    //按比例设置
    if(fixHeight){ iWidth = (iHeight = fixHeight) * scale; }
    if(fixWidth && (!fixHeight || iWidth > fixWidth)){ iHeight = (iWidth = fixWidth) / scale; }
    //返回尺寸对象
    return { Width: iWidth, Height: iHeight }
  }
}

经典论坛交流
http://bbs.blueidea.com/thread-2900687-1-1.html

本文链接:http://www.blueidea.com/tech/web/2008/6317.asp 

出处:蓝色理想
责任编辑:bluehearts

上一页 JavaScript 图片切割效果 [3] 下一页

◎进入论坛网页制作WEB标准化版块参加讨论,我还想发表评论

相关文章 更多相关链接
不唐突的JavaScript的七条准则
圆形光辉效果简明教程
超酷的金色组合
制作墙上文字效果简明教程
JavaScript 拖放效果分析
作者文章
JavaScript 拖放效果分析
关键字搜索 常规搜索 推荐文档
热门搜索:CSS Fireworks 设计比赛 网页制作 web标准 用户体验 UE photoshop Dreamweaver Studio8 Flash 手绘 CG
站点最新 站点最新列表
周大福“敬•自然”设计大赛开启
国际体验设计大会7月将在京举行
中国国防科技信息中心标志征集
云计算如何让安全问题可控
云计算是多数企业唯一拥抱互联网的机会
阿里行云
云手机年终巨献,送礼标配299起
阿里巴巴CTO王坚的"云和互联网观"
1499元买真八核 云OS双蛋大促
首届COCO桌面手机主题设计大赛
栏目最新 栏目最新列表
浅谈JavaScript编程语言的编码规范
如何在illustrator中绘制台历
Ps简单绘制一个可爱的铅笔图标
数据同步算法研究
用ps作简单的作品展示页面
CSS定位机制之一:普通流
25个最佳最闪亮的Eclipse开发项目
Illustrator中制作针线缝制文字效果
Photoshop制作印刷凹凸字体
VS2010中创建自定义SQL Rule
>> 分页 首页 前页 后页 尾页 页次:4/41个记录/页 转到 页 共4个记录

蓝色理想版权申明:除部分特别声明不要转载,或者授权我站独家播发的文章外,大家可以自由转载我站点的原创文章,但原作者和来自我站的链接必须保留(非我站原创的,按照原来自一节,自行链接)。文章版权归我站和作者共有。

转载要求:转载之图片、文件,链接请不要盗链到本站,且不准打上各自站点的水印,亦不能抹去我站点水印。

特别注意:本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有,文章若有侵犯作者版权,请与我们联系,我们将立即删除修改。

您的评论
用户名:  口令:
说明:输入正确的用户名和密码才能参与评论。如果您不是本站会员,你可以注册 为本站会员。
注意:文章中的链接、内容等需要修改的错误,请用报告错误,以利文档及时修改。
不评分 1 2 3 4 5
注意:请不要在评论中含与内容无关的广告链接,违者封ID
请您注意:
·不良评论请用报告管理员,以利管理员及时删除。
·尊重网上道德,遵守中华人民共和国的各项有关法律法规
·承担一切因您的行为而直接或间接导致的民事或刑事法律责任
·本站评论管理人员有权保留或删除其管辖评论中的任意内容
·您在本站发表的作品,本站有权在网站内转载或引用
·参与本评论即表明您已经阅读并接受上述条款
推荐文档 | 打印文档 | 评论文档 | 报告错误  
专业书推荐 更多内容
网站可用性测试及优化指南
《写给大家看的色彩书1》
《跟我去香港》
众妙之门—网站UI 设计之道
《Flex 4.0 RIA开发宝典》
《赢在设计》
犀利开发—jQuery内核详解与实践
作品集 更多内容

杂⑦杂⑧ Gold NORMANA V2