JavaScript 實現圖片點擊放大、文本點擊複製

以下是參考代碼,刪除過部分與主題沒關係的代碼 

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <meta name="viewport" content="initial-scale=1.0,user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>流量、話費充值</title>
    <link rel="stylesheet" id="wp-block-library-css" href="static/style.min.css" type="text/css" media="all">
    <link rel="stylesheet" id="style-css" href="static/main.css" type="text/css" media="all">
    <link rel="stylesheet" href="static/font-awesome.min.css" type="text/css" media="all">
    <script type="text/javascript" src="static/jquery.js"></script>
    <link rel="canonical" href="">
    <style type="text/css">
        .logo a:before {
            opacity: 1
        }

        html {
            padding: 1px;
        }

    </style>
</head>

<body class="" id="body-header-top">

<h2 class="btt mobies">
    <i class="fa fa-gittip" style="color: #E53A40;"></i>充值號碼
    <span οnclick="copyFun(this);"><b>12345679321563665</b></span>
</h2>

<div class="update_area">
    <p><br></p>
    <div class="update_area_content" style="text-align: center;margin-top: 10px;">
        <img src="1.jpg" class="pic" style="height: 130px;height: 130px;">
        <img src="2.jpg" class="pic" style="height: 130px;height: 130px;">
    </div>
    <p><br></p>
    <div style="padding: 5px;">
        <span style="color: red;">提示:支付時請備註上充值號</span>

        <input id="copyValue" type="text" value=""
               style="position: absolute;top: 0;left: 0;opacity: 0;z-index: -10;"/>
    </div>
    <div id="outerdiv"
         style="position:fixed;top:0;left:0;background:rgba(0,0,0,0.7);z-index:2;width:100%;height:100%;display:none;">
        <div id="innerdiv" style="position:absolute;">
            <img id="bigimg" style="border:5px solid #fff;" src=""/>
        </div>
    </div>

    <div style="padding: 5px;margin-top: 10px;">
        <p>充值步驟如下:</p>
        <p>1、點擊充值號複製</p>
        <p>2、在此頁面更具充值面額點擊對應二維碼</p>
        <p>3、識別或者掃馬備進如付款支付界面</p>
        <p>4、備註上你的設備編號或者號碼進行付款</p>
        <p>5、付款成功後這邊會爲你進行代理充</p>
        <p><br></p>
        <p>注:</p>
        <p>1、如果你忘記備註或者有誤請掃碼添加客服;</p>
        <p>2、你也可以直接添加客服爲你進行代理充值</p>
        <p><br></p>
        <p>客服二維碼:</p>
    </div>
    <div class="update_area_content" style="text-align: center;margin-top: 10px;">
        <img src="k1.jpg" class="pic" style="height: 130px;height: 130px;">
        <img src="k2.jpg" class="pic" style="height: 130px;height: 130px;">
    </div>
</div>
<br/>


<script language="JavaScript">
    $(function () {
        $(document).on('click', '.pic', function () {
            var _this = $(this);//將當前的pimg元素作爲_this傳入函數
            imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);
        });
    });

    function copyFun(ElementObj) {
        //獲取點擊的值
        var clickContent = ElementObj.innerText;
        $('#copyValue').val(clickContent);
        var inputElement = document.getElementById("copyValue").select();
        document.execCommand("Copy");
        alert('已複製', 1000);
    }

    function imgShow(outerdiv, innerdiv, bigimg, _this) {
        var src = _this.attr("src");//獲取當前點擊的pimg元素中的src屬性
        $(bigimg).attr("src", src);//設置#bigimg元素的src屬性

        /*獲取當前點擊圖片的真實大小,並顯示彈出層及大圖*/
        $("<img/>").attr("src", src).load(function () {
            var windowW = $(window).width();//獲取當前窗口寬度
            var windowH = $(window).height();//獲取當前窗口高度
            var realWidth = this.width;//獲取圖片真實寬度
            var realHeight = this.height;//獲取圖片真實高度
            var imgWidth, imgHeight;
            var scale = 0.8;//縮放尺寸,當圖片真實寬度和高度大於窗口寬度和高度時進行縮放

            if (realHeight > windowH * scale) {//判斷圖片高度
                imgHeight = windowH * scale;//如大於窗口高度,圖片高度進行縮放
                imgWidth = imgHeight / realHeight * realWidth;//等比例縮放寬度
                if (imgWidth > windowW * scale) {//如寬度扔大於窗口寬度
                    imgWidth = windowW * scale;//再對寬度進行縮放
                }
            } else if (realWidth > windowW * scale) {//如圖片高度合適,判斷圖片寬度
                imgWidth = windowW * scale;//如大於窗口寬度,圖片寬度進行縮放
                imgHeight = imgWidth / realWidth * realHeight;//等比例縮放高度
            } else {//如果圖片真實高度和寬度都符合要求,高寬不變
                imgWidth = realWidth;
                imgHeight = realHeight;
            }
            $(bigimg).css("width", imgWidth);//以最終的寬度對圖片縮放

            var w = (windowW - imgWidth) / 2;//計算圖片與窗口左邊距
            var h = (windowH - imgHeight) / 2;//計算圖片與窗口上邊距
            $(innerdiv).css({"top": h, "left": w});//設置#innerdiv的top和left屬性
            $(outerdiv).fadeIn("fast");//淡入顯示#outerdiv及.pimg
        });

        $(outerdiv).click(function () {//再次點擊淡出消失彈出層
            $(this).fadeOut("fast");
        });
    }
</script>
</body>
</html>

 

1.點擊充值碼複製 

 

2.點擊二維碼放大

 

 

參考博客:

https://www.cnblogs.com/wisewrong/p/7473978.html

https://www.cnblogs.com/yunshangwuyou/p/10538504.html

發佈了101 篇原創文章 · 獲贊 16 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章