当前位置:首页 > 程序心得 > 正文内容

Jq 网页点击图片放大效果(单张)

admin9年前 (2016-07-26)程序心得1824

首先引入jq文件:  <script src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>

$(function(){
    $('.slideBox1 p span img,.slideBox1 p img,.slideBox1 img').click(function(){
        var url = $(this).attr('src');//如果不想添加orgurl路径或者说src属性就是实际的路径这里可以改成$(this).attr('src')
        showBigImage(url,$(this));
    });
})
function showBigImage(url,obj){
    var maxWidth = 800;   //限制图片的最大宽度
    var offset = obj.offset();
    var start = {
        width:obj.attr('width')||parseInt(obj.css('width')),
        height:obj.attr('height')||parseInt(obj.css('height'))
    }; 
    $('<div></div>').attr('id', 'div_img_cover')
    	.css({ 'position': 'absolute', 'top': '0', 'left': '0', 'z-index': 9999, 
	        'width': "100%", 
	        'height': getWaitHeight(),
	        'text-align': 'center',
	        'font': 'bold 24px arial',
	        'background-repeat': 'no-repeat',
	        'background-position': 'center',
	        'background-attachment': 'fixed',
	        'opacity':0.8,
	        'background':'#000'
	        })
	    .appendTo('body');
    
    
    var img = new Image();
    $(img).css(start);
    if(img.attachEvent){
        img.attachEvent('onload',imgLoaded)
    }else{
        img.addEventListener('load',imgLoaded,false);
    }
    img.src = url;

    function imgLoaded(){
        var imgWidth = img.width*1.5,imgHeight = img.height*1.5;    //设置图片放大的比例,最好保持宽高放大等比
        var newWidth = imgWidth > maxWidth ? maxWidth : imgWidth;
        var newHeight = imgHeight/imgWidth*newWidth
        var end = {
            width:parseInt(newWidth),
            height:parseInt(newHeight)
        }
        var container = $('<div></div>').appendTo('body');
        container.css('position','absolute');
        container.css(offset);
        container.attr('id','container');
        container.append(img);
        var css = setElementCenter('#container',1,end);
        $(img).animate(end,200);
        container.animate(css,200);
        $('#div_img_cover').click(function(){
            var that = $(this);
            $(img).animate(start,200);
            container.animate(offset,200,function(){
                that.remove();
                $(this).remove();
            });
        });
    }
    
    function setElementCenter(selector,pos,finalSize){
        var position = $(selector).css('position')=='fixed';
        var winSize = myGetWinSize();
        var elementSize = finalSize || getElementSize(selector);
        var finalTop =(winSize.height-elementSize.height)/2 + parseInt((!position ? winSize.scrollTop : 0)),
            finalLeft=(winSize.width-elementSize.width)/2+winSize.scrollLeft;
        var finalCss = {
            left:finalLeft,
            top:position ? finalTop : (finalTop<winSize.scrollTop ? winSize.scrollTop : finalTop)
        }
        if(!pos){
            $(selector).css({'position':position ?'fixed' : 'absolute','z-index':10000});
            $(selector).css(finalCss);
        }
        $(selector).css({'z-index':10000});
        return finalCss;
    }
    function myGetWinSize(){
        var size = {
            width:document.documentElement.clientWidth || document.body.clientWidth,
            height:document.documentElement.clientHeight || document.body.clientHeight,
            scrollTop:$(window).scrollTop(),
            scrollLeft:$(window).scrollLeft()
        }
        return size;
    }
    function getElementSize(selector){
        return {
            width:$(selector).width(),
            height:$(selector).height()
        }
    }
    function getWaitHeight(){
        var scrollHeight,
            offsetHeight;
        // handle IE 6
        if ($.browser.msie && $.browser.version < 7) {
            scrollHeight = Math.max(
                 document.documentElement.scrollHeight,
                 document.body.scrollHeight
            );
            offsetHeight = Math.max(
                     document.documentElement.offsetHeight,
                     document.body.offsetHeight
            );
        
            if (scrollHeight < offsetHeight) {
                     return $(window).height() + 'px';
            } else {
                     return scrollHeight + 'px';
            }
        // handle "good" browsers
        } else {
            return $(document).height() + 'px';
        }
    }}
</script>


效果如下:

blob.png

分享到:

扫描二维码推送至手机访问。

版权声明:本文由云河空间发布,如需转载请注明出处。

本文链接:https://yuyunhe.cn/index.php/post/167.html

分享给朋友:

“Jq 网页点击图片放大效果(单张)” 的相关文章

SVN Skipped 'xxx' -- Node remains in conflict 错误的解决办法

svn提交发现错误#cd /home/svn/app/# svn upUpdating '.':Skipped 'xxx' -- Node remains in conflictAt revision 1054.Summary of conflicts: ...

Mysql 触发器使用(含navicat使用案例)

Mysql 触发器使用(含navicat使用案例)

CREATE TRIGGER updateutime2 BEFORE UPDATE on yp_cms_score for EACH ROW BEGIN  SET NEW.utime= ...

多行文字溢出[...]的实现(text-overflow: ellipsis)

多行文字溢出[...]的实现(text-overflow: ellipsis)

对于单行文字, 很简单. Css代码  .oneLine {      width: 200px;      overflow:&...

css做屏幕适配

CSS判断不同分辨率浏览器(显示屏幕)显示不同宽度布局CSS3技术支持IE6到IE8。将用到css3 @media样式进行判断,但IE9以下版本不支持CSS3技术,这里DIVCSS5给大家介绍通过JS实现低版本的浏览器也支持CSS3实现实用布局CSS DIV网页布局中当分辨率小于等于1024px(像...

RSA密钥的生成与配置(支付宝公私密钥可用)

RSA密钥的生成与配置(支付宝公私密钥可用)

RSA密钥的生成与配置openssl下载地址http://dldx.csdn.net/fd.php?i=20313208579480&s=ac2e809e168f7d5b8bf1515d3d6b1aa4,或者官方下载通过openssl工具生成RSA的公钥和私钥(opnssl工具可在互联网中下...

微信支付 总提示get_brand_wcpay_request:fail 也不跳转支付页面 的解决方案

微信支付 总提示get_brand_wcpay_request:fail 也不跳转支付页面 的解决方案

最近在做微信支付,帮客户部署好环境后,测试微信支付,发现点击支付后老是提示:get_brand_wcpay_request:fail,于是找到代码中调用微信支付的代码段:WeixinJSBridge.invoke('getBrandWCPayRequest', { &nb...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。