vue a simple package assembly time div marquee

I recorded some time ago about a package of components vue bar. Technology need to accumulate, before I have time I write pretty good out of the components are open source. Vue component package and to try and react in two ways. Today wrote a simple mouse marquee div effect of selected package of it.

div marquee achieve

div marquee effect, in fact, there is no good way is to get the mouse events, depending on the position of the mouse, creating a dynamic follow the mouse div. [Note: This method relies on positioning mode position, the general position is for global mouse events, it is best to position the mouse marquee of the parent element div position is to locate the root element. Otherwise, the mouse marquee marquee area and the area is very difficult to be consistent. ]

In fact, it is summed up in two steps:

Press and hold the left mouse button, move the mouse appears rectangular box;

Release the mouse button, DOM elements based on the statistical rectangular marquee box emerging upper range;

Follow the mouse create a div, the code is as follows:

 //  创建选框节点
      this.selectBoxDashed = document.createElement('div') this.selectBoxDashed.className = 'haorooms-select-box' document.body.appendChild(this.selectBoxDashed) this.scrollX = document.documentElement.scrollLeft || document.body.scrollLeft this.scrollY = document.documentElement.scrollTop || document.body.scrollTop // 设置选框的初始位置 this.startX = e.x + this.scrollX || e.clientX + this.scrollX // e是鼠标事件的event this.startY = e.y + this.scrollY || e.clientY + this.scrollY // e是鼠标事件的event this.selectBoxDashed.style.cssText = `left:${this.startX}px;top:${this.startY}px`

Mouse move, get a list of the selected div, add a temporary class

  this.selectBoxDashed.style.display = 'block' // 上面创建的鼠标跟随div出现 // 根据鼠标移动,设置选框的位置、宽高 this.initx = e.x + this.scrollX || e.clientX + this.scrollX //鼠标移动的初始位置+滚动轴的位置 this.inity = e.y + this.scrollY || e.clientY + this.scrollY // 暂存选框的位置及宽高,用于将 select-item 选中 this.left = Math.min(this.initx, this.startX) this.top = Math.min(this.inity, this.startY) this.width = Math.abs(this.initx - this.startX) this.height = Math.abs(this.inity - this.startY) this.selectBoxDashed.style.left = `${this.left}px` this.selectBoxDashed.style.top = `${this.top}px` this.selectBoxDashed.style.width = `${this.width}px` this.selectBoxDashed.style.height = `${this.height}px` let fileDivs = document.getElementsByClassName('list') // 获取要选中的div列表 for (let i = 0; i < fileDivs.length; i++) { let itemX_pos = fileDivs[i].offsetWidth + fileDivs[i].offsetLeft let itemY_pos = fileDivs[i].offsetHeight + fileDivs[i].offsetTop let condition1 = itemX_pos > this.left let condition2 = itemY_pos > this.top let condition3 = fileDivs[i].offsetLeft < (this.left + this.width) let condition4 = fileDivs[i].offsetTop < (this.top + this.height) if (condition1 && condition2 && condition3 && condition4) {// 在框选范围之内 fileDivs[i].classList.add('temp-selected') } else { fileDivs[i].classList.remove('temp-selected') } }

Lift the mouse, select the class increase

 let selectDom = document.getElementsByClassName('temp-selected'); [].slice.call(selectDom).forEach(item => { if (item.classList.contains('selected')) { item.classList.remove('selected') } else { item.classList.add('selected') } item.classList.remove('temp-selected') }) if (this.selectBoxDashed) { try { this.selectBoxDashed.parentNode.removeChild(this.selectBoxDashed) } catch (err) { // console.log(err) } } let fileDivs = document.getElementsByClassName('list') // 这里是改变数据 for (let i = 0; i < fileDivs.length; i++) { if (fileDivs[i].classList.contains('selected')) { this.timeList[i] = '1' } else { this.timeList[i] = '0' } }

The results are as follows:

enter image description here

Code published to npm

This component, including I wrote before vue mobile terminal drop-down load the next component data are published to the npm,

npm Address: https://www.npmjs.com/package/timedivselect

use:

npm install timedivselect -S

import timeDivSelect from 'timedivselect'

Use examples:

https://www.jkys120.com

By the way encountered a small problem npm publish it

Before I npm also released some on how to publish npm package, although there is no written before I blog, but many online.

The first time with the generally:

npm adduser

// 输入用户名,密码等【npm 网站要提前注册,npm网站的用户名和密码】

Not the first time

npm login

Publish, delete, etc.

npm publish // 发布

npm unpublish 包名 // 撤销删除

Back to the topic, the problem I encountered today is npm: no_perms Private mode enable, only admin can publish this module

Before publishing are good, why suddenly this conference have this error message? The original is because I've specified npm Taobao mirror.

Currently recommended NRM

sudo npm install -g nrm

View source list

nrm ls

Use a source

nrm use npm

Such a re-release either.

Guess you like

Origin www.cnblogs.com/zqw111/p/11295525.html