模块化编程——单选框封装--12.14


一、基类组件Componet.js

用来创建指定元素,并设置父容器。

export default class Component extends EventTarget{
    elem;
    constructor(type){
        super();
        this.elem=document.createElement(type);
    }
    appendTo(parent){
        if(typeof parent==="string") parent=document.querySelector(parent);
        if(parent)parent.appendChild(this.elem);
        return parent;
    }
}

二、多选框类,CheckBox.js

1.引入父类

代码如下(示例):

import Component from "./Component.js";

2.CheckBox.js

其中,initStyle()设置初始化样式,setStyle()修改点击后的样式。disatch()触发事件。
代码如下(示例):

export default class CheckBox extends Component{
    _checked=false;
    constructor(label){
       super("div");
       this.elem.innerHTML=`
        <div class='icon'></div>
        <div class='label'>${label}</div>
       `
       this.elem.style.display="flex";
       this.initStyle();
       this.elem.addEventListener("click",e=>this.clickHandler(e));
    }
    initStyle(){
        Object.assign(this.elem.firstElementChild.style,{
            width:"14px",
            height:"14px",
            backgroundImage:"url(./img/new_icon.png)",
            backgroundPositionX:"-238px",
            backgroundPositionY:"-37px",
            marginTop:"3px",
            marginRight:"5px"
       });
    }
    clickHandler(e){
        this.setChecked(!this._checked);
        this.dipatch();
    }
    dipatch(){
        var evt=new Event("change");
        evt.checked=this._checked;
        this.dispatchEvent(evt);
    }
    setChecked(bool){
        this._checked=bool;
        this.setStyle();
    }
    setStyle(){
        Object.assign(this.elem.firstElementChild.style,{
            backgroundPositionX:this._checked ? "-128px" : "-238px",
            backgroundPositionY:this._checked ? "-126px" : "-37px",
       });
    }
}

三、单选框类

export default class Radio extends CheckBox{
    // 存储不同组的选中的信息,组名
    group;
    // 添加一个静态属性,用来存储每组中的不同value
    // list={sex:['男','女'],hobby:['唱歌','跳舞']};
    static group_list={};
    constructor(label,group){
        super(label);
        // 继承父类生成个div;
        this.group=group;
        if(!Radio.group_list[group]){
            // console.log(this.group,'不存在');
            // 不存在 将group添加到静态属性中。将当前选中的对象,赋值给当前group
            Radio.group_list[group]=this;
            // 添加选中状态
            this.setChecked(true);
        }
    }
    // 更改当前div点击事件
    clickHandler(e){
        // 将之前group中存储的对象,取消选中状态
        if(Radio.group_list[this.group]) Radio.group_list[this.group].setChecked(false);
        // 更改当前group中存储的对象为当前点击对象,并设置选中状态为true
        Radio.group_list[this.group]=this;
        this.setChecked(true);
        this.dipatch();//抛发事件
    }

    // 初始化样式
    initStyle(){
        Object.assign(this.elem.firstElementChild.style,{
            width:"18px",
            height:"18px",
            backgroundImage:"url(./img/new_icon.png)",
            backgroundPositionX:"-195px",
            backgroundPositionY:"-104px",
            marginTop:"2px",
            marginRight:"8px"
       });
    }

    // 更改样式
    setStyle(){
        Object.assign(this.elem.firstElementChild.style,{
            backgroundPositionX:this._checked ? "-175px" : "-195px"
       });
    }

四、 HTML

    <script type='module'>
        import Radio from './js/Radio.js';
        var arr=['男','女'];
        arr.forEach(item=>{
            var rb=new Radio(item,'sex');
            rb.appendTo('body');
        })
    </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值