首页 > 其他 > 详细

面向对象-矩形

时间:2019-02-11 15:27:24      阅读:163      评论:0      收藏:0      [点我收藏+]
// html
<div id="main"></div>

// js

function Rect (options) {
  this._init(options)
}
Rect.prototype = {
  // 属性
  _init: function (options) {
    // 父标签
    this.parentId = options.parentId
    // 位置
    this.left = options.left || 100
    this.top = options.top || 100
    // 自身的属性
    this.width = options.width || 100
    this.height = options.height || 50
    this.bgColor = options.bgColor || ‘#000‘
    this.borderRadius = options.borderRadius || 0
    this.border = options.border || ‘none‘
  },

  // 行为
  render: function () {
    // 1.获取父标签
    var parentEle = document.getElementById(this.parentId)
    // 2.创建 矩形标签
    var ele = document.createElement(‘div‘)
    parentEle.appendChild(ele)

    ele.style.position = ‘absolute‘
    ele.style.left = this.left + ‘px‘
    ele.style.top = this.top + ‘px‘

    ele.style.width = this.width + ‘px‘
    ele.style.height = this.height + ‘px‘

    ele.style.backgroundColor = this.bgColor
    ele.style.border = this.border
    ele.style.borderRadius = this.borderRadius + ‘px‘
  }
}

var rect = new Rect({
  parentId: ‘main‘,
  width: 500,
  height: 300,
  bgColor: ‘pink‘
})
// console.log(rect)
rect.render()

 

面向对象-矩形

原文:https://www.cnblogs.com/musi03/p/10361951.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!