美文网首页
class语法相关笔记

class语法相关笔记

作者: UmustHU | 来源:发表于2018-08-24 15:06 被阅读0次

用class写的一个小demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        *{margin:0;padding:0;}
        .box{
            position:absolute;
            top:0;
            width:100px;
            height:100px;
            background-color:cornflowerblue;
        }
        .left{left:0;}
        .right{right:0;}
    </style>
</head>
<body>
<div id="div1" class="box left"></div>
<div id="div2" class="box right"></div>
<script>
    class Drag{
        constructor(id){
            this.oDiv = document.querySelector(id);
            this.disX = 0;
            this.disY = 0;
            this.init()
        }
        init(){
            this.oDiv.onmousedown = function(event){
                this.disX = event.clientX - this.oDiv.offsetLeft;
                this.disY = event.clientY - this.oDiv.offsetTop;

                document.onmousemove = this.fnMove.bind(this);
                document.onmouseup = this.fnUp.bind(this);
            }.bind(this);
        }
        fnMove(event){
            this.oDiv.style.left = event.clientX - this.disX + 'px';
            this.oDiv.style.top = event.clientY - this.disY + 'px';
        }
        fnUp(){
            document.onmousemove = null;
            document.onmouseup = null;
        }
    }
    //从父级继承
    class LimitDrag extends Drag{
        fnMove(event){
            super.fnMove(event);//执行父级的fnMove函数
            //以下内容为专属子级的操作
            if(this.oDiv.offsetLeft <= 0){
                this.oDiv.style.left = 0;
            }
            if(this.oDiv.offsetTop <= 0){
                this.oDiv.style.top = 0;
            }
        }
    }
    new Drag('#div1')
    new LimitDrag('#div2')
</script>
</body>
</html>

相关文章

  • class语法相关笔记

    用class写的一个小demo

  • React 学习笔记

    React 笔记 一、JSX 语法 疑难点class => classNamefor...

  • Class的基本语法(笔记)

    js与es6对比 1.js中没有类(class)这个概念,通过构造函数的方式实例化对象。 2.es6中,引入类这个...

  • v-bind 及 class 与 style 绑定

    class的绑定 对象语法 数组语法 数组语法中可以使用对象语法 style的绑定 绑定内联样式与绑定class的...

  • Dart相关语法笔记

    1. 级联操作符 2.引用库冲突 2.1 as 关键字 同一个函数处于两个包中,同时我们都引用了这两个包,类似下面...

  • Day9:Vue文档精读3——渲染&事件

    class与style的绑定 绑定HTML Class 对象语法 数组语法 用在组件上 绑定内联样式 对象语法 数...

  • Class 与 Style 如何动态绑定?

    Class 与 Style 如何动态绑定? Class 可以通过对象语法和数组语法进行动态绑定: 对象语法: 数组...

  • Class语法

    ES6前 类的实例 getter和setter 属性表达式 Class表达式 注意点 静态方法 实例属性新写法 静...

  • python dict() 函数使用

    dict() 函数用于创建一个字典。 语法 语法: class dict(**kwargs)class dict(...

  • class 相关

    class 基本用法[https://es6.ruanyifeng.com/#docs/class] static...

网友评论

      本文标题:class语法相关笔记

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