jquery图片查看器

稿件来源: 阳光企业网站管理系统   撰稿作者: 太阳光   发表日期: 2013-02-22   阅读次数: 1727   查看权限: 游客查看

弹窗式图片查看,含有图片旋转、图片缩放等功能。

    说明:
    1.点击图片弹出可移动的展示窗口;
    2.当图片宽度小于指定值(默认200)即不再支持鼠标滚轮缩小;
    3.图片可90度居中旋转;
    4.非IE下当旋转90度或270度时缩放不能中心固定(bug需要改进);
    5.支持大部分浏览器
    难点:图片旋转兼容,图片旋转后IE浏览器默认图片的长与宽互换,非IE浏览器不互换,中心点也难以捉摸。

查看效果点击这里 相关阅读:jquery xzy_zoompic 图片放大缩小查看插件

<script type="text/javascript"> 
    $(document).ready(function() {
        setTimeout(function(){
            $.each($(".z_pic"),function(){
                var i=$(this).height();
                if(i>80){
                    $(this).css({height:"80px",width:"auto"});
                }else{
                    $(this).css({marginTop:parseInt(40-i/2)+"px"});
                }
            });
        },1000);
        /*弹出div的宽,高,缩放倍数,最小的缩放尺寸*/
        $(".z_pic").xzy_zoom(600,500,5,200);
    });
    jQuery.fn.extend({
        xzy_zoom:function (w,h,unit,min){
            w=w||300;
            h=h||300;
            unit=unit||5;
            min=min||200;
            var g_l,g_t,g_id="xzy_zoom_pic";
            $('body').append('<div id="xzy_zoom"><div id="xzy_zoom_title"><ul><li id="xzy_zoom_left" title="左转90度"></li><li id="xzy_zoom_right" title="右转90度"></li><li id="xzy_zoom_close" title="点击关闭"></li></ul><span></span></div><img src="" id="'+g_id+'" /></div>');
            $("#xzy_zoom_title").width(w-4);
            $("#xzy_zoom").css({height:h-22,width:w});
            $(this).click(function(){
                rotate_pic("restore");
                var url=$(this).attr("src");
                var t=$(this).offset().top;
                var img = new Image();
                img.onload=function(){
                    g_l=Math.floor((w-this.width)/2);
                    g_t=Math.floor((h-this.height)/2);
                    $("#"+g_id).attr("src",url).css({width:"auto",height:"auto",left:g_l,top:g_t,marginLeft:0,marginTop:0});
                    $("#xzy_zoom").css({display:"block",left:Math.floor($(window).width()/2-w/2),top:t});
                };
                img.src=url;
                $("#xzy_zoom_title span").html($(this).attr("alt"));
            });
            function move_div(id){
                $(id).mousedown(function (e) {
                    var oe=e||window.event;
                    var $this = document.getElementById($(this).attr("id"));
                    var startX = oe.clientX - $this.offsetLeft+parseFloat($(this).css("marginLeft"));
                    var startY = oe.clientY - $this.offsetTop+parseFloat($(this).css("marginTop"));
                    document.onmousemove = function (e) {
                        var oe = e || window.event;
                        $this.style.left = oe.clientX - startX + "px";
                        $this.style.top = oe.clientY - startY + "px";
                    };
                    document.onmouseup = function () {
                        document.onmousemove = document.onmouseup = null;
                        if ($this.releaseCapture)$this.releaseCapture();
                    };
                    if ($this.setCapture)$this.setCapture();
                    return false;
                });
            }
            function scrollMouse(e) {
                var direct = 0;
                e = e || window.event;
                if (e.wheelDelta) {
                    direct = e.wheelDelta;
                    e.returnValue=false
                } else if (e.detail) {
                    direct = e.detail;
                    e.preventDefault();
                }
                var W = $(this).width();
                var ratio=$(this).height()/W;
                var _t =$(this).position().top;
                var _l =$(this).position().left;
                if (direct < 0) {
                    W += unit;
                    _t-= unit/2*ratio;
                    _l-= unit/2;
                } else {
                    W -= unit;
                    _t+= unit/2*ratio;
                    _l+= unit/2;
                }
                if(W>min){
                    if(i_rotate%180==0)
                        $(this).css({height:"auto",width:W,top:_t,left:_l});
                    else{
                        if(isIE)
                            $(this).css({height:W,width:"auto",top:_t,left:_l});
                        else
                            $(this).css({width:W});//非IE下难以居中缩放
                    }
                }
                return false;
            }
            $("#"+g_id).mouseover(function(){
                if (document.addEventListener) {
                    this.addEventListener('DOMMouseScroll', scrollMouse, false);
                }
                this.onmousewheel = scrollMouse;
            }).dblclick(function(){
                rotate_pic("restore");
                $(this).css({width:"auto",height:"auto",left:g_l,top:g_t});
            });
            move_div("#"+g_id);
            move_div("#xzy_zoom");
            $("#xzy_zoom_close").click(function(){
                $("#xzy_zoom").hide();
            });
            $("#xzy_zoom_left").click(function(){rotate_pic(-90)});
            $("#xzy_zoom_right").click(function(){rotate_pic(90)});
            var i_rotate=0,
                userAgent = navigator.userAgent,
                isIE = /msie/i.test(userAgent) && !window.opera,
                isWebKit = /webkit/i.test(userAgent),
                isFirefox = /firefox/i.test(userAgent);
            function rotate_pic(n) {
                if(isNaN(n)){
                    i_rotate=0;
                }else{
                    i_rotate+=n;
                }
                var degree=i_rotate%360;
                var temp=Math.abs(degree);
                var target=document.getElementById(g_id);
                if (isWebKit){
                    target.style.webkitTransform = "rotate("+degree+"deg)";
                } else if (isFirefox){
                    target.style.MozTransform = "rotate("+degree+"deg)";
                } else if (isIE) {
                    degree = degree / 180 * Math.PI;
                    var sinDeg = Math.sin(degree);
                    var cosDeg = Math.cos(degree);
                    target.style.filter = "progid:DXImageTransform.Microsoft.Matrix(M11="+cosDeg+",M12="+(-sinDeg)+",M21="+sinDeg+",M22="+cosDeg+",SizingMethod='auto expand')";
                    //解决旋转居中
                    var i_w=target.width,i_h=target.height,i_m=Math.max(i_w,i_h);
                    if(temp%180==0){
                        $("#"+g_id).css({marginLeft:0,marginTop:0});
                    }else{
                        $("#"+g_id).css({marginLeft:(i_m-i_h)/2,marginTop:(i_h-i_m)/2});
                    }
                } else {
                    target.style.transform = "rotate("+degree+"deg)";
                }
            }
        }
    });
</script>

 

关键词: jquery,图片,图片旋转缩放   编辑时间: 2013-09-03 16:01:27

  • 感到高兴

    1

    高兴
  • 感到支持

    0

    支持
  • 感到搞笑

    0

    搞笑
  • 感到不解

    0

    不解
  • 感到谎言

    0

    谎言
  • 感到枪稿

    0

    枪稿
  • 感到震惊

    0

    震惊
  • 感到无奈

    0

    无奈
  • 感到无聊

    0

    无聊
  • 感到反对

    0

    反对
  • 感到愤怒

    0

    愤怒
100%(5)
0%(0)
共有0 条评论 发言请遵守【相关规定

网友评论

会员头像
发 表同步腾讯微博    验证码:  点击更新请先登陆
  • 暂无评论
关闭模块文章图片 article Pictrue
  • 我的妈妈爸爸
  • 基于koa2+mysql+vue2.0+Element阳光内容管理系统
  • 代码覆盖率工具 Istanbul 入门教程
  • 全栈工程师的武器——MEAN
  • 9款超炫的 CSS3 复选框(Checkbox)
  • 微信开发在线翻译功能
  • CSS3那些不为人知的高级属性
  • 给easyui的datebox添加清空事件
  • flash写字效果
  • kendoUI系列教程之DropDownList下拉菜单
  • kendoUI系列教程之datetimepicker日期时间选择
  • kendoUI系列教程之datepicker日期选择
  • kendoUI系列教程之combobox下拉列表框
  • kendoUI系列教程之colorpicker
  • kendoUI系列教程之calendar日历表
  • kendoUI系列教程之autocomplete自动补齐