一个原生JavaScript实现的简易数据视图单向绑定实例

218 篇文章 18 订阅
208 篇文章 8 订阅

<div id="app">
  <h2 class="title" data-on="title"></h2>
  <div class="content" data-on="content"></div>
  <div class="count" data-on="count"></div>
  <div class="control">
    <label for="content-input">请输入内容:</label>
    <input type="text" class="content-input" placeholder="请输入内容" id="content-input" /></div>
  <button class="add" onclick="">加1</button>
</div>


<div class="skip">
  <a href="https://codepen.io/collection/DRKGbz/" target="_blank">Vue案例全集</a>
</div>
@import url(https://fonts.googleapis.com/css?family=Montserrat);

$colour:hsla(350,100%,25%,1);
$grey:desaturate($colour,90%);

body {
  background:lighten($grey,30%);
    background-image:
    linear-gradient(40deg,transparentize($grey,0.95),transparentize($colour,0.95)),

    linear-gradient(135deg,
        lighten($grey,18%) 0%,
        lighten($grey,18%) 10%,
        lighten($grey,25%) 11%,
        lighten($grey,25%) 40%,
        lighten($grey,35%) 41%,
        lighten($grey,35%) 50%,
        lighten($grey,18%) 51%,
        lighten($grey,18%) 60%,
        lighten($grey,25%) 61%,
        lighten($grey,25%) 90%,
        lighten($grey,35%) 91%)
    ;
    background-size:7px 7px, 4px 4px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: montserrat;
}

html,body {
  width: 100vw;
  height: 100vh;
}

#app {
  background: white;
	border: 0 none;
	border-radius: 3px;
	box-shadow: 0 0 15px 1px rgba(0, 0, 0, 0.4);
	padding: 20px 30px;
	box-sizing: border-box;
  text-align: center;
}
.title {
  margin: 0 0 25px;
  display: inline-block;
  border-bottom: 2px solid #69f;
  padding-bottom: 5px;
  color: #69f;
  text-shadow: 1px 1px 0 rgba(#000, .4);
}
.control {
  margin-top: 20px;
  margin-bottom: 20px;
  label{
    display: inline-block;
    vertical-align: middle;
  }
  input {
    height: 40px;
    padding: 5px 10px;
    box-sizing: border-box;
    border-radius: 3px;
    border: 1px solid rgba(#000, .5);
    box-shadow: 0 0 3px rgba(#000, .15);
    
    &:focus,
    &:active {
      outline: none 0;
      box-shadow: 0 0 4px rgba(0, 169, 244, .9);
      border-color: rgba(0, 169, 244, 1);
    }
  }  
}

button {
  color: #454545;
  background: transparent;
  border: 2px solid #454545;
  position: relative;
  margin: 1em auto;
  padding: 0.5em 1em;
  transition: all 0.3s ease-in-out;
  text-align: center;
  font-family: comfortaa;
  font-weight: bold;
  font-size: 2rem;
  cursor: pointer;
}
button:before, button:after {
  content: '';
  display: block;
  position: absolute;
  border-color: #454545;
  box-sizing: border-box;
  border-style: solid;
  width: 1em;
  height: 1em;
  transition: all 0.3s ease-in-out;
}
button:before {
  top: -6px;
  left: -6px;
  border-width: 2px 0 0 2px;
  z-index: 5;
}
button:after {
  bottom: -6px;
  right: -6px;
  border-width: 0 2px 2px 0;
}
button:hover:before, button:hover:after {
  width: calc(100% + 12px);
  height: calc(100% + 12px);
  border-color: #fff;
}
button:hover {
  color: #353535;
  background-color: #fff;
}
button:active, button:focus {
  outline: none;
}
.content {
  margin: 15px auto 20px;
}

.count {
  font-size: 2rem;
  display: inline-block;
  vertical-align: middle;
}

.skip{
  position: fixed;
  right: 0;
  bottom: 0;
  padding: 10px 30px;
  background: rgba(0,0,0,.5);
  
  a {
    color: #fff;
    text-decoration: none;
    text-shadow: 1px 1px 0 rgba(0, 0, 0, .15);
  }
}

 

(function() {
  // 数据
  window.data = {
    title: '数据视图单向绑定',
    content: '使用属性描述器实现数据视图绑定',
    count: 0
  };
  var attr = 'data-on'; // 约定好的语法,声明DOM绑定对象属性

  // 使用发布/订阅模式,集中管理监控和触发回调事件
  var Observer = {
    watchers: {},
    subscribe: function(key) {
      var el = document.querySelector('[' + attr + '="'+ key + '"]');

      // demo
      var cb = function react(val) {
        el.innerHTML = val;
      }

      if (this.watchers[key]) {
        this.watchers[key].push(cb);
      } else {
        this.watchers[key] = [].concat(cb);
      }
  },
  emit: function(key, val) {
    var len = this.watchers[key] && this.watchers[key].length;

    if (len && len > 0) {
      for(var i = 0; i < len; i++) {
        this.watchers[key][i](val);
      }
    }
   }
  };

  // 为对象中每一个属性设置描述对象,尤其是存取器函数
  function defineDescriptors(obj) {
    for (var key in obj) {
      // 遍历属性
      defineDescriptor(obj, key, obj[key]);
    }

    // 为特定属性设置描述对象
    function defineDescriptor(obj, key, val) {
      Object.defineProperty(obj, key, {
        enumerable: true,
        configurable: true,
        get: function() {
          var value = val;
          return value;
        },
        set: function(newVal) {
          if (newVal !== val) {
            // 值发生变更才执行
            val = newVal;
            Observer.emit(key, newVal); // 触发更新DOM
          }
        }
      });
      Observer.subscribe(key); // 为该属性注册回调
    }
  }

  // 初始化demo
  function init() {
    defineDescriptors(data); // 处理数据对象
    var eles = document.querySelectorAll('[' + attr + ']');

    // 初始遍历DOM展示数据
    // 其实可以将该操作放到属性描述对象的get方法内,则在初始化时只需要对属性遍历访问即可
    for (var i = 0, len = eles.length; i < len; i++) {
      eles[i].innerHTML = data[eles[i].getAttribute(attr)];
    }

    // 辅助测试实例
    document.querySelector('.add').addEventListener('click', function(e) {
      data.count += 1;
    });
    document.querySelector('.content-input').addEventListener('input', function(e) {
      data.content = e.target.value;
    });
  }
  init();
})();

转自https://codepen.io/airen/pen/dZREXr

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
东南亚位于我国倡导推进的“一带一路”海陆交汇地带,作为当今全球发展最为迅速的地区之一,近年来区域内生产总值实现了显著且稳定的增长。根据东盟主要经济体公布的最新数据,印度尼西亚2023年国内生产总值(GDP)增长5.05%;越南2023年经济增长5.05%;马来西亚2023年经济增速为3.7%;泰国2023年经济增长1.9%;新加坡2023年经济增长1.1%;柬埔寨2023年经济增速预计为5.6%。 东盟国家在“一带一路”沿线国家中的总体GDP经济规模、贸易总额与国外直接投资均为最大,因此有着举足轻重的地位和作用。当前,东盟与中国已互相成为双方最大的交易伙伴。中国-东盟贸易总额已从2013年的443亿元增长至 2023年合计超逾6.4万亿元,占中国外贸总值的15.4%。在过去20余年中,东盟国家不断在全球多变的格局里面临挑战并寻求机遇。2023东盟国家主要经济体受到国内消费、国外投资、货币政策、旅游业复苏、和大宗商品出口价企稳等方面的提振,经济显现出稳步增长态势和强韧性的潜能。 本调研报告旨在深度挖掘东南亚市场的增长潜力与发展机会,分析东南亚市场竞争态势、销售模式、客户偏好、整体市场营商环境,为国内企业出海开展业务提供客观参考意见。 本文核心内容: 市场空间:全球行业市场空间、东南亚市场发展空间。 竞争态势:全球份额,东南亚市场企业份额。 销售模式:东南亚市场销售模式、本地代理商 客户情况:东南亚本地客户及偏好分析 营商环境:东南亚营商环境分析 本文纳入的企业包括国外及印尼本土企业,以及相关上下游企业等,部分名单 QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。邮箱:market@qyresearch.com

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值