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

推荐阅读更多精彩内容