JS代码总结

作者: aclihui | 来源:发表于2018-12-20 21:56 被阅读10次

原文地址 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

相关文章

  • JSPatch下发笔记10

    OC代码: JS代码: 总结:NSForegroundColorAttributeName 在js中要写成"NSC...

  • JS代码总结

    原文地址 https://www.aclihui.com js网页代码总结 为什么写 当下的网页js有很多,但是总...

  • 私人tool.js

    个人总结的一些js工具代码

  • JSPatch下发笔记6

    OC代码: JS代码: 总结:1.不可变数组表示方法,OC:@[image],JS:[image]。2.字典取值方...

  • JSPatch下发笔记3

    OC代码: JS代码: 总结:1.扩展类的写法:如:NSString (Float) 写成defineClass(...

  • WebView与JS交互

    1、交互方式总结 2、Android调用JS 方式1:通过WebView的loadUrl()JS代码: Andro...

  • vue.config.js前端解决跨域配置(node后端)

    demo 前端代码: node后端: 总结:前端这边先根据request.js去vue.config.js里面去找...

  • JSPatch下发笔记7

    oc代码: JS代码: 总结: 当在for循环中用到block的时候,在JS中,block里取到的永远是i的最大值...

  • 2020-12-02 CSS 文件的代码分割(4.92)

    之前总结的代码分割都是针对JS代码的,如果要对CSS代码进行代码分割,要怎么办呢?借助MiniCssExtract...

  • JSPatch下发笔记4

    OC代码: JS代码: 总结:1.修改多个类中的代码,用defineClass("");分隔。2.遇到枚举值用原值代替

网友评论

    本文标题:JS代码总结

    本文链接:https://www.haomeiwen.com/subject/vzoukqtx.html