JS补-最主要的是js情书

推荐网站:
http://www.w3school.com.cn/htmldom/index.asp
https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000
https://www.cnblogs.com/suoning/p/5656922.html
https://www.cnblogs.com/suoning/p/5656403.html

一、js,dom与浏览器之间的关系
DOM理解:
文档对象模型(Document Object Model,DOM)是一种用于HTML和XML文档的编程接口, (DOM) 是中立于平台和语言的接口,它允许程序和脚本动态地访问和更新文档的内容、结构和样式。
js理解:javascript是一门动态弱类型的解释型编程语言,增强页面动态效果,实现页面与用户之间的实时动态的交互(JavaScript是一门 基于浏览器的 、 事件触发式的 、 嵌入式的… 脚本语言)
关系:浏览器内部有一个js的解释器/执行器/引擎,浏览器自带HTML代码的渲染功能,HTML代码在内存中形成一个DOM对象(HTML对象),html代码中的js代码被js引擎所执行,执行的结果就是DOM操作;
二、js语法
1、引入
①、外部引入

<script type="text/javascript" src="JS文件"></script>

②内部引入

<script type="text/javascript">
    Js代码内容
</script> 

③、 为什么要放在<body>代码块底部?

HTML代码从上到下执行,先加载CSS,避免html出现无样式状态;
将JavaScript代码块放在<body>最后,可以让网页尽快的呈现给用户,减少浏览者的等待时间,避免因为JS代码块阻塞网页的呈现

三、js对象(js名言‘万物皆对象’)

如上图所示:万维网w3c对js的分类;

如上图所示:我觉得js只存在一种对象就是BOM对象(毕竟JavaScript是一门 基于浏览器的 、 事件触发式的 、 嵌入式的… 脚本语言),而DOM再分为普通js对象,DOM对象,Window对象;

js情书:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<title></title>

<style type="text/css">
@font-face {
    font-family: digit;
    src: url('digital-7_mono.ttf') format("truetype");
}
body{margin:0;padding:0;background:#ffe;font-size:12px;overflow:auto}
#mainDiv{width:100%;height:100%}
#loveHeart{float:left;width:670px;height:625px}
#garden{width:100%;height:100%}
#elapseClock{text-align:right;font-size:18px;margin-top:10px;margin-bottom:10px}
#words{font-family:"sans-serif";width:500px;font-size:24px;color:#666}
#messages{display:none}
#elapseClock .digit{font-family:"digit";font-size:36px}
#loveu{padding:5px;font-size:22px;margin-top:80px;margin-right:120px;text-align:right;display:none}
#loveu .signature{margin-top:10px;font-size:20px;font-style:italic}
#clickSound{display:none}
#code{float:left;width:440px;height:400px;color:#333;font-family:"Consolas","Monaco","Bitstream Vera Sans Mono","Courier New","sans-serif";font-size:12px}
#code .string{color:#2a36ff}
#code .keyword{color:#7f0055;font-weight:bold}
#code .placeholder{margin-left:15px}#code .space{margin-left:7px}
#code .comments{color:#3f7f5f}
#copyright{margin-top:10px;text-align:center;width:100%;color:#666}
#errorMsg{width:100%;text-align:center;font-size:24px;position:absolute;top:100px;left:0}
#copyright a{color:#666}
</style>


<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" >
    function Vector(x, y) {
            this.x = x;
            this.y = y;
        };
        
        Vector.prototype = {
            rotate: function (theta) {
                var x = this.x;
                var y = this.y;
                this.x = Math.cos(theta) * x - Math.sin(theta) * y;
                this.y = Math.sin(theta) * x + Math.cos(theta) * y;
                return this;
            },
            mult: function (f) {
                this.x *= f;
                this.y *= f;
                return this;
            },
            clone: function () {
                return new Vector(this.x, this.y);
            },
            length: function () {
                return Math.sqrt(this.x * this.x + this.y * this.y);
            },
            subtract: function (v) {
                this.x -= v.x;
                this.y -= v.y;
                return this;
            },
            set: function (x, y) {
                this.x = x;
                this.y = y;
                return this;
            }
        };
        
        function Petal(stretchA, stretchB, startAngle, angle, growFactor, bloom) {
            this.stretchA = stretchA;
            this.stretchB = stretchB;
            this.startAngle = startAngle;
            this.angle = angle;
            this.bloom = bloom;
            this.growFactor = growFactor;
            this.r = 1;
            this.isfinished = false;
            //this.tanAngleA = Garden.random(-Garden.degrad(Garden.options.tanAngle), Garden.degrad(Garden.options.tanAngle));
            //this.tanAngleB = Garden.random(-Garden.degrad(Garden.options.tanAngle), Garden.degrad(Garden.options.tanAngle));
        }
        Petal.prototype = {
            draw: function () {
                var ctx = this.bloom.garden.ctx;
                var v1, v2, v3, v4;
                v1 = new Vector(0, this.r).rotate(Garden.degrad(this.startAngle));
                v2 = v1.clone().rotate(Garden.degrad(this.angle));
                v3 = v1.clone().mult(this.stretchA); //.rotate(this.tanAngleA);
                v4 = v2.clone().mult(this.stretchB); //.rotate(this.tanAngleB);
                ctx.strokeStyle = this.bloom.c;
                ctx.beginPath();
                ctx.moveTo(v1.x, v1.y);
                ctx.bezierCurveTo(v3.x, v3.y, v4.x, v4.y, v2.x, v2.y);
                ctx.stroke();
            },
            render: function () {
                if (this.r <= this.bloom.r) {
                    this.r += this.growFactor; // / 10;
                    this.draw();
                } else {
                    this.isfinished = true;
                }
            }
        }

        function Bloom(p, r, c, pc, garden) {
            this.p = p;
            this.r = r;
            this.c = c;
            this.pc = pc;
            this.petals = [];
            this.garden = garden;
            this.init();
            this.garden.addBloom(this);
        }
        Bloom.prototype = {
            draw: function () {
                var p, isfinished = true;
                this.garden.ctx.save();
                this.garden.ctx.translate(this.p.x, this.p.y);
                for (var i = 0; i < this.petals.length; i++) {
                    p = this.petals[i];
                    p.render();
                    isfinished *= p.isfinished;
                }
                this.garden.ctx.restore();
                if (isfinished == true) {
                    this.garden.removeBloom(this);
                }
            },
            init: function () {
                var angle = 360 / this.pc;
                var startAngle = Garden.randomInt(0, 90);
                for (var i = 0; i < this.pc; i++) {
                    this.petals.push(new Petal(Garden.random(Garden.options.petalStretch.min, Garden.options.petalStretch.max), Garden.random(Garden.options.petalStretch.min, Garden.options.petalStretch.max), startAngle + i * angle, angle, Garden.random(Garden.options.growFactor.min, Garden.options.growFactor.max), this));
                }
            }
        }

        function Garden(ctx, element) {
            this.blooms = [];
            this.element = element;
            this.ctx = ctx;
        }
        Garden.prototype = {
            render: function () {
                for (var i = 0; i < this.blooms.length; i++) {
                    this.blooms[i].draw();
                }
            },
            addBloom: function (b) {
                this.blooms.push(b);
            },
            removeBloom: function (b) {
                var bloom;
                for (var i = 0; i < this.blooms.length; i++) {
                    bloom = this.blooms[i];
                    if (bloom === b) {
                        this.blooms.splice(i, 1);
                        return this;
                    }
                }
            },
            createRandomBloom: function (x, y) {
                this.createBloom(x, y, Garden.randomInt(Garden.options.bloomRadius.min, Garden.options.bloomRadius.max), Garden.randomrgba(Garden.options.color.rmin, Garden.options.color.rmax, Garden.options.color.gmin, Garden.options.color.gmax, Garden.options.color.bmin, Garden.options.color.bmax, Garden.options.color.opacity), Garden.randomInt(Garden.options.petalCount.min, Garden.options.petalCount.max));
            },
            createBloom: function (x, y, r, c, pc) {
                new Bloom(new Vector(x, y), r, c, pc, this);
            },
            clear: function () {
                this.blooms = [];
                this.ctx.clearRect(0, 0, this.element.width, this.element.height);
            }
        }

        Garden.options = {
            petalCount: {
                min: 8,
                max: 15
            },
            petalStretch: {
                min: 0.1,
                max: 3
            },
            growFactor: {
                min: 0.1,
                max: 1
            },
            bloomRadius: {
                min: 8,
                max: 10
            },
            density: 10,
            growSpeed: 1000 / 60,
            color: {
                rmin: 128,
                rmax: 255,
                gmin: 0,
                gmax: 128,
                bmin: 0,
                bmax: 128,
                opacity: 0.1
            },
            tanAngle: 60
        };
        Garden.random = function (min, max) {
            return Math.random() * (max - min) + min;
        };
        Garden.randomInt = function (min, max) {
            return Math.floor(Math.random() * (max - min + 1)) + min;
        };
        Garden.circle = 2 * Math.PI;
        Garden.degrad = function (angle) {
            return Garden.circle / 360 * angle;
        };
        Garden.raddeg = function (angle) {
            return angle / Garden.circle * 360;
        };
        Garden.rgba = function (r, g, b, a) {
            return 'rgba(' + r + ',' + g + ',' + b + ',' + a + ')';
        };
        Garden.randomrgba = function (rmin, rmax, gmin, gmax, bmin, bmax, a) {
            var r = Math.round(Garden.random(rmin, rmax));
            var g = Math.round(Garden.random(gmin, gmax));
            var b = Math.round(Garden.random(bmin, bmax));
            var limit = 5;
            if (Math.abs(r - g) <= limit && Math.abs(g - b) <= limit && Math.abs(b - r) <= limit) {
                return Garden.rgba(rmin, rmax, gmin, gmax, bmin, bmax, a);
            } else {
                return Garden.rgba(r, g, b, a);
            }
        };
</script>
<script type="text/javascript" >
        var $window = $(window), gardenCtx, gardenCanvas, $garden, garden;
        var clientWidth = $(window).width();
        var clientHeight = $(window).height();

        $(function () {
            // setup garden
            $loveHeart = $("#loveHeart");
            var offsetX = $loveHeart.width() / 2;
            var offsetY = $loveHeart.height() / 2 - 55;
            $garden = $("#garden");
            gardenCanvas = $garden[0];
            gardenCanvas.width = $("#loveHeart").width();
            gardenCanvas.height = $("#loveHeart").height()
            gardenCtx = gardenCanvas.getContext("2d");
            gardenCtx.globalCompositeOperation = "lighter";
            garden = new Garden(gardenCtx, gardenCanvas);
            
            $("#content").css("width", $loveHeart.width() + $("#code").width());
            $("#content").css("height", Math.max($loveHeart.height(), $("#code").height()));
            $("#content").css("margin-top", Math.max(($window.height() - $("#content").height()) / 2, 10));
            $("#content").css("margin-left", Math.max(($window.width() - $("#content").width()) / 2, 10));

            // renderLoop
            setInterval(function () {
                garden.render();
            }, Garden.options.growSpeed);
        });

        $(window).resize(function() {
            var newWidth = $(window).width();
            var newHeight = $(window).height();
            if (newWidth != clientWidth && newHeight != clientHeight) {
                location.replace(location);
            }
        });

        function getHeartPoint(angle) {
            var t = angle / Math.PI;
            var x = 19.5 * (16 * Math.pow(Math.sin(t), 3));
            var y = - 20 * (13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t));
            return new Array(offsetX + x, offsetY + y);
        }

        function startHeartAnimation() {
            var interval = 50;
            var angle = 10;
            var heart = new Array();
            var animationTimer = setInterval(function () {
                var bloom = getHeartPoint(angle);
                var draw = true;
                for (var i = 0; i < heart.length; i++) {
                    var p = heart[i];
                    var distance = Math.sqrt(Math.pow(p[0] - bloom[0], 2) + Math.pow(p[1] - bloom[1], 2));
                    if (distance < Garden.options.bloomRadius.max * 1.3) {
                        draw = false;
                        break;
                    }
                }
                if (draw) {
                    heart.push(bloom);
                    garden.createRandomBloom(bloom[0], bloom[1]);
                }
                if (angle >= 30) {
                    clearInterval(animationTimer);
                    showMessages();
                } else {
                    angle += 0.2;
                }
            }, interval);
        }

        (function($) {
            $.fn.typewriter = function() {
                this.each(function() {
                    var $ele = $(this), str = $ele.html(), progress = 0;
                    $ele.html('');
                    var timer = setInterval(function() {
                        var current = str.substr(progress, 1);
                        if (current == '<') {
                            progress = str.indexOf('>', progress) + 1;
                        } else {
                            progress++;
                        }
                        $ele.html(str.substring(0, progress) + (progress & 1 ? '_' : ''));
                        if (progress >= str.length) {
                            clearInterval(timer);
                        }
                    }, 75);
                });
                return this;
            };
        })(jQuery);

        function timeElapse(date){
            var current = Date();
            var seconds = (Date.parse(current) - Date.parse(date)) / 1000;
            var days = Math.floor(seconds / (3600 * 24));
            seconds = seconds % (3600 * 24);
            var hours = Math.floor(seconds / 3600);
            if (hours < 10) {
                hours = "0" + hours;
            }
            seconds = seconds % 3600;
            var minutes = Math.floor(seconds / 60);
            if (minutes < 10) {
                minutes = "0" + minutes;
            }
            seconds = seconds % 60;
            if (seconds < 10) {
                seconds = "0" + seconds;
            }
            var result = "<span class=\"digit\">" + days + "</span> days <span class=\"digit\">" + hours + "</span> hours <span class=\"digit\">" + minutes + "</span> minutes <span class=\"digit\">" + seconds + "</span> seconds"; 
            $("#elapseClock").html(result);
        }

        function showMessages() {
            adjustWordsPosition();
            $('#messages').fadeIn(5000, function() {
                showLoveU();
            });
        }

        function adjustWordsPosition() {
            $('#words').css("position", "absolute");
            $('#words').css("top", $("#garden").position().top + 195);
            $('#words').css("left", $("#garden").position().left + 70);
        }

        function adjustCodePosition() {
            $('#code').css("margin-top", ($("#garden").height() - $("#code").height()) / 2);
        }

        function showLoveU() {
            $('#loveu').fadeIn(3000);
        }
</script>

</head>

<body>

<div id="mainDiv">
    <div id="content">
        <div id="code">
            <span class="comments">/**</span><br />
            <span class="space"/><span class="comments">*,</span><br />
            <span class="space"/><span class="comments">*.</span><br />
            <span class="space"/><span class="comments">*/</span><br />
            Boy name = <span class="keyword">Mr</span> HE<br />
            Girl name = <span class="keyword">Mrs</span> WANG<br />
            <span class="comments">// Fall in love river. </span><br />
            The boy love the girl;<br />
            <span class="comments">// They love each other.</span><br />
            The girl loved the boy;<br />
            <span class="comments">// AS time goes on.</span><br />
            The boy can not be separated the girl;<br />
            <span class="comments">// At the same time.</span><br />
            The girl can not be separated the boy;<br />
            <span class="comments">// Both wind and snow all over the sky.</span><br />
            <span class="comments">// Whether on foot or 5 kilometers.</span><br />
            <span class="keyword">The boy</span> very <span class="keyword">happy</span>;<br />
            <span class="keyword">The girl</span> is also very <span class="keyword">happy</span>;<br />
            <span class="placeholder"/><span class="comments">// Whether it is right now</span><br />
            <span class="placeholder"/><span class="comments">// Still in the distant future.</span><br />
            <span class="placeholder"/>The boy has but one dream;<br />
            <span class="comments">// The boy wants the girl could well have been happy.</span><br />
            <br>
            <br>
            I want to say:<br />
            Baby, I love you forever;<br />
        </div>
        <div id="loveHeart">
            <canvas id="garden"></canvas>
            <div id="words">
                <div id="messages">
                    亲爱的,这是我们相爱在一起的时光。
                    <div id="elapseClock"></div>
                </div>
                <div id="loveu">
                    爱你直到永永远远。<br/>
                    <div class="signature">- 爱你的人</div>
                </div>
            </div>
        </div>
    </div>
    <div id="copyright">
        <a href="#">....</a><br />
        <a href="#">....</a><br />
        
    </div>
</div>

<script type="text/javascript">
var offsetX = $("#loveHeart").width() / 2;
var offsetY = $("#loveHeart").height() / 2 - 55;
var together = new Date();
together.setFullYear(2013, 2, 28);
together.setHours(20);
together.setMinutes(0);
together.setSeconds(0);
together.setMilliseconds(0);

if (!document.createElement('canvas').getContext) {
    var msg = document.createElement("div");
    msg.id = "errorMsg";
    msg.innerHTML = "Your browser doesn't support HTML5!<br/>Recommend use Chrome 14+/IE 9+/Firefox 7+/Safari 4+"; 
    document.body.appendChild(msg);
    $("#code").css("display", "none")
    $("#copyright").css("position", "absolute");
    $("#copyright").css("bottom", "10px");
    document.execCommand("stop");
} else {
    setTimeout(function () {
        startHeartAnimation();
    }, 5000);

    timeElapse(together);
    setInterval(function () {
        timeElapse(together);
    }, 500);

    adjustCodePosition();
    $("#code").typewriter();
}
</script>

</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,530评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,403评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,120评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,770评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,758评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,649评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,021评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,675评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,931评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,751评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,410评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,004评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,969评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,042评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,493评论 2 343

推荐阅读更多精彩内容