zoukankan      html  css  js  c++  java
  • 前端学习笔记之使用面相对象实现图形自由移动并随机切换颜色

     
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
            .box {
                 160px;
                height: 160px;
                border-radius: 50%;
                position: absolute;
                left: 0;
                top: 0;
                background: repeating-linear-gradient(135deg, #fff 0, #fff 10px, green 10px, green 20px);
            }
            .box2 {
                 80px;
                height: 40px;
                border-radius: 15px;
                background-color: #fff;
                position: absolute;
                left: 40px;
                top: 60px;
                z-index: 1;
            }
            h1 {
                position: absolute;
                text-align: center;
                line-height: 40px;
                top: -22px;
                left: 10px;
                z-index: 11;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <div class="box2">
                <h1>520</h1>
            </div>
        </div>
        <script>
            class Box {
                constructor(selector) {
                    this.node = document.querySelector("div");
                }
                get width() {
                    return this.node.offsetWidth;
                }
                set width(v) {
                    this.node.style.width = v + "px"
                }
                get height() {
                    return this.node.offsetHeight;
                }
                set width(v) {
                    this.node.style.height = v + "px"
                }
                get left() {
                    return this.node.offsetLeft;
                }
                set left(v) {
                    this.node.style.left = v + "px"
                }
                get top() {
                    return this.node.offsetTop;
                }
                set top(v) {
                    this.node.style.top = v + "px"
                }
                // get color() {
                //     return getComputedStyle(this.node).color;
                // }
                // set color(v) {
                //     this.node.style.color = v
                // }
            }
            const b1 = new Box(".box");
            setInterval(() => {
                b1.left += 3;
                b1.top += 1;
                b1.width += 1;
                b1.height += 1;
            }, 150);
            function changeColor() {
                let color = "#f00|#0f0|#00f|#880|#808|#088|yellow|green|blue|gray";
                color = color.split("|");
                let c = document.getElementsByTagName("h1");
                for (let i = 0; i < c.length; i++) {
                    c[i].style.color = color[parseInt(Math.random() * color.length)];
                }
            }
            setInterval("changeColor()", 50);
            function random(min, max) {
                if (arguments.length === 0) {
                    return 0;
                } else if (arguments.length === 1) {
                    max = min;
                    min = 0;
                } else {
                    if (max < min) {
                        [min, max] = [max, min]
                    }
                }
                return parseInt(Math.random() * (max - min)) + min;
            }
        </script>
    </body>
    </html>
  • 相关阅读:
    VMware下Linux配置网络
    前端知识之查漏补缺二
    前端网络基础查漏补缺篇
    简单实现Promise
    前端知识之查漏补缺-1
    git tag
    云服务器安装node环境 mysql nginx
    js动画
    vue原理之双向绑定虚
    js的封装、继承与多态
  • 原文地址:https://www.cnblogs.com/Yangyecool/p/13171644.html
Copyright © 2011-2022 走看看