JS代碼總結

原文地址 https://www.aclihui.com

js網頁代碼總結

爲什麼寫

當下的網頁js有很多,但是總結起來的非常的少,於是乎此文就是爲總結js網頁代碼的一片文章。

代碼區域

標題欄切換特效

<!--標題欄切換特效 start-->
<script type="text/javascript">
var OriginTitile = document.title;
var titleTime;
document.addEventListener('visibilitychange', function() {
if (document.hidden) {
$('[rel="shortcut icon"]').attr('href', "//www.hzv5.cn/logo.jpg");
document.title = '(⊙_⊙) 哎呀!崩潰啦~ •' + OriginTitile;
clearTimeout(titleTime);
}
else {
$('[rel="shortcut icon"]').attr('href', "//hzv5.cn/logo.jpg");
document.title = '๑乛◡乛๑ 又好啦,嘿嘿~ •' + OriginTitile;
titleTime = setTimeout(function() {
document.title = OriginTitile;
}, 1500);
}
});
</script>
<!--標題欄切換特效 end-->

給複製內容增加版權信息

<!--給複製內容增加版權信息 start-->
<script>
    $("body").on('copy', function (e) {
    if (typeof window.getSelection == "undefined") return;
    var body_element = document.getElementsByTagName('body')[0];
    var selection = window.getSelection();
    if (("" + selection).length < 30) return;
    var newdiv = document.createElement('div');
    newdiv.style.position = 'absolute';
    newdiv.style.left = '-99999px';
    body_element.appendChild(newdiv);
    newdiv.appendChild(selection.getRangeAt(0).cloneContents());
    //提示覆製成功代碼start
    if(window.getSelection){
        selection = window.getSelection();
        alert("複製成功~\n若轉載請註明出處:"+document.location.href);
        //document.write("複製成功~\n轉載請註明出處:"+document.location.href);
        }else if(document.getSelection){//IE10
        selection= document.getSelection();
        alert("複製成功~\n若轉載請註明出處:"+document.location.href);
        }else if(document.selection){//IE6+10-
        selection= document.selection.createRange().text;
        alert("複製成功~\n若轉載請註明出處:"+document.location.href);
        }else{
        selection= "";
        alert("瀏覽器兼容問題導致複製失敗!");
        }
        //提示覆製成功代碼end
    if (selection.getRangeAt(0).commonAncestorContainer.nodeName == "PRE") {
        newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
    }
    newdiv.innerHTML += "</br></br>若轉載請註明出處: <a href='" + document.location.href + "'>" + document.location.href + "</a> &copy; hzv5.cn";
    selection.selectAllChildren(newdiv);
    window.setTimeout(function () { body_element.removeChild(newdiv); }, 200);
});
</script>
<!--給複製內容增加版權信息 end-->

鼠標點擊出現隨機數字

<!--鼠標點擊出現隨機數字 start-->
<script type="text/javascript">
var a_idx = 0;
jQuery(document).ready(function($) {
    $("body").click(function(e) {
        var a = new Array("天真","富強", "民主", "文明", "和諧", "自由", "平等", "公正" ,"法治", "愛國", "敬業", "誠信", "友善");
        var $i = $("<span/>").text(a[a_idx]);
        a_idx = (a_idx + 1) % a.length;
        var x = e.pageX,
        y = e.pageY;
        $i.css({
            "z-index": 99999,
            "top": y - 20,
            "left": x,
            "position": "absolute",
            "font-weight": "bold",
            "color": "#ff0000"
        });
        $("body").append($i);
        $i.animate({
            "top": y - 180,
            "opacity": 0
        },
        1800,
        function() {
            $i.remove();
        });
    });
});


(function(window, document, undefined) {
    var hearts = [];
    window.requestAnimationFrame = (function() {
        return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
        function(callback) {
            setTimeout(callback, 1000 / 60);
        }
    })();
    init();
    function init() {
        css(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
        attachEvent();
        gameloop();
    }
    function gameloop() {
        for (var i = 0; i < hearts.length; i++) {
            if (hearts[i].alpha <= 0) {
                document.body.removeChild(hearts[i].el);
                hearts.splice(i, 1);
                continue;
            }
            hearts[i].y-= 1.6;
            hearts[i].scale += 0.01;
            hearts[i].alpha -= 0.01;
            hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
        }
        requestAnimationFrame(gameloop);
    }
    function attachEvent() {
        var old = typeof window.onclick === "function" && window.onclick;
        window.onclick = function(event) {
            old && old();
            createHeart(event);
        }
    }
    function createHeart(event) {
        var d = document.createElement("div");
        d.className = "heart";
        hearts.push({
            el: d,
            x: event.clientX - 2,
            y: event.clientY - 2,
            scale: 1,
            alpha: 1,
            color: randomColor()
        });
        document.body.appendChild(d);
    }
    function css(css) {
        var style = document.createElement("style");
        style.type = "text/css";
        try {
            style.appendChild(document.createTextNode(css));
        } catch (ex) {
            style.styleSheet.cssText = css;
        }
        document.getElementsByTagName('head')[0].appendChild(style);
    }
    function randomColor() {
        return "rgb(" + (~~ (Math.random() * 255)) + "," + (~~ (Math.random() * 255)) + "," + (~~ (Math.random() * 255)) + ")";
    }
})(window, document);
</script>

<!--鼠標點擊出現隨機數字 end-->

來源: http://www.hzv5.cn 的源代碼run

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章