(() => '');
Vue.nextTick( () => {
const createNode = handleFn();
const fWarpElm = el._tipElm;
if (fWarpElm) {
fWarpElm.replaceChild(createNode, el._createElm);
el._createElm = createNode;
const offset = elemOffset(fWarpElm);
const location = position(el);
fWarpElm.style.cssText = `left: ${location.left - offset.width / 2}px; top: ${location.top - top - offset.height}px;`;
}
})
},
// 删除 事件
unbind (el , bind , vnode) {
off({
el: dov.body,
type: 'mouseup',
fn: el._tip_mouse_up_fn
});
el = null;
}
}
slider 组件
<template>
<p class="jc-slider-cmp">
<section
class="slider-active-bg"
:style="{width: `${left}px`}"
>
</section>
<i
class="jc-slider-dot"
:style="{left: `${left}px`}"
ref="dot"
@mousedown="moveStart"
v-jc-tips="createNode"
>
</i>
</p>
</template>
<script>
import {elemOffset, on, off, once} from "../../utils/dom";
const global = window;
const doc = global.document;
export default {
props: {
step: {
type: [Number],
default: 0
},
rangeEnd: {
type: [Number],
required: true
},
value: {
type: [Number],
required: true
},
minValue: {
type: [Number],
required: true
},
maxValue: {
type: [Number],
required: true
}
},
data () {
return {
startX: null,
width: null,
curValue: 0,
curStep: 0,
left: 0,
tempLeft: 0
}
},
computed: {
wTov () {
let step = this.step;
let width = this.width;
let rangeEnd = this.rangeEnd;
if (width) {
if (step) {
return width / (rangeEnd / step)
}
return width / rangeEnd
}
return null
},
postValue () {
let value = null;
if (this.step) {
value = this.step * this.curStep;
} else {
value = this.left / this.wTov;
}
return value;
}
},
watch: {
value: {
handler (value) {
this.$nextTick(() => {
let step = this.step;
let wTov = this.wTov;
if (step) {
this.left = value / step * wTov;
} else {
this.left = value * wTov;
}
})
},
immediate: true
}
},
methods: {
moveStart (e) {
e.preventDefault();
const body = window.document.body;
const _this = this;
this.startX = e.pageX;
this.tempLeft = this.left;
on({
el: body,
type: 'mousemove',
fn: this.moving
});
once({
el: body,
type: 'mouseup',
fn: function() {
console.log('end');
_this.$emit('input', _this.postValue);
off({
el: body,
type: 'mousemove',
fn: _this.moving
})
}
})
},
moving(e) {
let curX = e.pageX;
let step = this.step;
let rangeEnd = this.rangeEnd;
let width = this.width;
let tempLeft = this.tempLeft;
let startX = this.startX;
let wTov = this.wTov;
if (step !== 0) {
let all = parseInt(rangeEnd / step);
let curStep = (tempLeft + curX - startX) / wTov;
curStep > all && (curStep = all);
curStep < 0 && (curStep = 0);
curStep = Math.round(curStep);
this.curStep = curStep;
this.left = curStep * wTov;
} else {
let left = tempLeft + curX - startX;
left < 0 && (left = 0);
left > width && (left = width);
this.left = left;
}
},
createNode () {
const fElem = document.createElement('i');
const textNode = document.createTextNode(this.postValue.toFixed(2));
fElem.appendChild(textNode);
return fElem;
}
},
mounted () {
this.width = elemOffset(this.$el).width;
}
};
</script>
<style lang="scss">
.jc-slider-cmp {
position: relative;
display: inline-block;
width: 100%;
border-radius: 4px;
height: 8px;
background: #ccc;
.jc-slider-dot {
position: absolute;
display: inline-block;
width: 15px;
height: 15px;
border-radius: 50%;
left: 0;
top: 50%;
transform: translate(-50%, -50%);
background: #333;
cursor: pointer;
}
.slider-active-bg {
position: relative;
height: 100%;
border-radius: 4px;
background: red;
}
}
</style>../utils/dom
const global = window;
export const on = ({el, type, fn}) => {
if (typeof global) {
if (global.addEventListener) {
el.addEventListener(type, fn, false)
} else {
el.attachEvent(`on${type}`, fn)
}
}
};
export const off = ({el, type, fn}) => {
if (typeof global) {
if (global.removeEventListener) {
el.removeEventListener(type, fn)
} else {
el.detachEvent(`on${type}`, fn)
}
}
};
export const once = ({el, type, fn}) => {
const hyFn = (event) => {
try {
fn(event)
}
finally {
off({el, type, fn: hyFn})
}
}
on({el, type, fn: hyFn})
};
// 最后一个
export const fbTwice = ({fn, time = 300}) => {
let [cTime, k] = [null, null]
// 获取当前时间
const getTime = () => new Date().getTime()
// 混合函数
const hyFn = () => {
const ags = argments
return () => {
clearTimeout(k)
k = cTime = null
fn(...ags)
}
};
return () => {
if (cTime == null) {
k = setTimeout(hyFn(...arguments), time)
cTime = getTime()
} else {
if ( getTime() - cTime < 0) {
// 清除之前的函数堆 ---- 重新记录
clearTimeout(k)
k = null
cTime = getTime()
k = setTimeout(hyFn(...arguments), time)
}
}}
};
export const contains = function(parentNode, childNode) {
if (parentNode.contains) {
return parentNode !== childNode && parentNode.contains(childNode)
} else {
// https://developer.mozilla.org/zh-CN/docs/Web/API/Node/compareDocumentPosition
return (parentNode.compareDocumentPosition(childNode) === 16)
}
};
export const addClass = function (el, className) {
if (typeof el !== "object") {
return null
}
let classList = el['className']
classList = classList === '' ? [] : classList.split(/\s+/)
if (classList.indexOf(className) === -1) {
classList.push(className)
el.className = classList.join(' ')
}
};
export const removeClass = function (el, className) {
let classList = el['className']
classList = classList === '' ? [] : classList.split(/\s+/)
classList = classList.filter(item => {
return item !== className
})
el.className = classList.join(' ')
};
export const delay = ({fn, time}) => {
let oT = null
let k = null
return () => {
// 当前时间
let cT = new Date().getTime()
const fixFn = () => {
k = oT = null
fn()
}
if (k === null) {
oT = cT
k = setTimeout(fixFn, time)
return
}
if (cT - oT < time) {
oT = cT
clearTimeout(k)
k = setTimeout(fixFn, time)
}
}
};
export const position = (son, parent = global.document.body) => {
let top = 0;
let left = 0;
let offsetParent = son;
while (offsetParent !== parent) {
let dx = offsetParent.offsetLeft;
let dy = offsetParent.offsetTop;
let old = offsetParent;
if (dx === null) {
return {
flag: false
}
}
left += dx;
top += dy;
offsetParent = offsetParent.offsetParent;
if (offsetParent === null && old !== global.document.body) {
return {
flag: false
}
}
}
return {
flag: true,
top,
left
}
};
export const getElem = (filter) => {
return Array.from(global.document.querySelectorAll(filter));
};
export const elemOffset = (elem) => {
return {
width: elem.offsetWidth,
height: elem.offsetHeight
}
};以上就是vue指令如何实现气泡提示(附代码)的详细内容,更多请关注php中文网其它相关文章!
网站建设是一个广义的术语,涵盖了许多不同的技能和学科中所使用的生产和维护的网站。
关键词:vue指令如何完成气泡提示(附代码)