js鼠标移入判断文本是否溢出,溢出并给出提示

vue 指令使用 v-title="{title: ‘’}"
文章中使用了es6语法

const camelCase = function(name) {
    return name.replace(SPECIAL_CHARS_REGEXP, function(_, separator, letter, offset) {
        return offset ? letter.toUpperCase() : letter;
    }).replace(MOZ_HACK_REGEXP, 'Moz$1');
};
function setStyle(element, styleName, value) {
    if (!element || !styleName) return;

    if (typeof styleName === 'object') {
        for (var prop in styleName) {
            if (styleName.hasOwnProperty(prop)) {
                setStyle(element, prop, styleName[prop]);
            }
        }
    } else {
        styleName = camelCase(styleName);
        if (styleName === 'opacity' && ieVersion < 9) {
            element.style.filter = isNaN(value) ? '' : 'alpha(opacity=' + value * 100 + ')';
        } else {
            element.style[styleName] = value;
        }
    }
};
var showOverflowHiddenText = (el,value) => {
 	if (el.offsetWidth < el.scrollWidth) {
 		let title = value;
 		if(!value){
 			var re1 = new RegExp("<.+?>", "g");
 			title = el.innerHTML.replace(re1, '');
 		}
        el.setAttribute("title", title);
    }
}
Vue.directive("title", {
    //溢出隐藏 并增加鼠标提示
     inserted: (el, binding, vnode) => {
        showOverflowHiddenText(el, binding.value ? binding.value.title : '')
     },
     componentUpdated: (el, binding, vnode) => {
     	showOverflowHiddenText(el, binding.value ? binding.value.title : '')
     }
 });
Logo

前往低代码交流专区

更多推荐