vue组件和js实现鼠标悬停显示title效果

news/2024/7/7 10:12:56

需求:

显示文本内容过长,显示…鼠标悬浮时,全部显示

  • 使用element组件<el-tooltip offset="-2" class="item" effect="dark" placement="top"> <span class="dispatchSystemAddressBookItemText">{{item.name}}</span> <div slot="content"> <div class="dispatchSystemAddressBookItemText-totip"> {{item.name}} </div> </div> </el-tooltip>
  • 在这里插入代码片
<div class="dispatchSystemAddressBook-itemBox">
  <div
          :class="[dispatchSystemAddressBookItemText ,index === currentSystemAddressBookItem ? selectDispatchSystemAddressBookItemText : nomalDispatchSystemAddressBookItemText]"
          v-for="(item,index) in table1PannelTest"
          :key="item.id"
          @click="handleDispatchTabClick(item.id,index)">
      <template v-if="GlobalFunc.getStringLength(item.name)>6">
     <el-tooltip offset="-2" class="item" effect="dark" placement="top">
              <span class="dispatchSystemAddressBookItemText">{{item.name}}</span>
              <div slot="content">
                  <div class="dispatchSystemAddressBookItemText-totip">
                      {{item.name}}
                  </div>
              </div>

          </el-tooltip>


      </template>
      <template v-else>
          <span :title="item.name" class="dispatchSystemAddressBookItemText">{{item.name}}</span>
      </template>

  </div>
</div>

element组件效果

在这里插入图片描述
在这里插入图片描述

一、部分需求推荐方案 使用js自定义悬浮

  • 全局挂载函数
  • 新建元素,添加样式
  • 组件使用

这里我们把定义函数挂载到全局
在这里插入图片描述


/**
 * 鼠标悬停显示TITLE
 * @params     obj        当前悬停的标签
 *
 */
GlobalFunc.titleMouseOver=function titleMouseOver(event,words_per_line) {
  //无TITLE悬停,直接返回
  if(typeof event.target.title == 'undefined' || event.target.title == '') return false;
  //不存在title_show标签则自动新建
  var title_show = document.getElementById("title_show");
  if(title_show == null){
    title_show = document.createElement("div");                            //新建Element
    document.getElementsByTagName('body')[0].appendChild(title_show);    //加入body中
    var attr_id = document.createAttribute('id');                        //新建Element的id属性
    attr_id.nodeValue = 'title_show';                                    //为id属性赋值
    title_show.setAttributeNode(attr_id);                                //为Element设置id属性
    document.getElementById("title_show").classList.add("title_focus_toolTip");//为为Element添加类属性
    var attr_style = document.createAttribute('style');                    //新建Element的style属性
    attr_style.nodeValue = 'position:absolute;'                            //绝对定位
      /*  +'border:solid 1px #f3f3f3; background:rgba(50, 50, 50, 0.701961)'                //边框、背景颜色
        +'top:-50%'                //边框、背景颜色
        +'border-radius:2px;box-shadow:0px 0px 2px #ccc;'                //圆角、阴影
        +'line-height:30px!important;'                                            //行间距
        +'font-size:18px; padding: 2px 5px;';                            //字体大小、内间距*/
    try{
      title_show.setAttributeNode(attr_style);                        //为Element设置style属性
    }catch(e){
      //IE6
      // title_show.style.position = 'absolute';
      // title_show.style.border = 'solid 1px #f3f3f3';
      // title_show.style.background = 'rgba(50, 50, 50, 0.701961)';
      // title_show.style.lineHeight = '20px';
      // title_show.style.fontSize = '30px';
      // title_show.style.padding = '2px 5px';
      document.getElementById("title_show").classList.add("title_focus_toolTip");//为为Element添加类属性
    }
  }
  //存储并删除原TITLE
  document.title_value = event.target.title;
  event.target.title = '';
  //单行字数未设定,非数值,则取默认值50
  if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
    words_per_line = 50;
  }
  //格式化成整形值
  words_per_line = parseInt(words_per_line);
  //在title_show中按每行限定字数显示标题内容,模拟TITLE悬停效果
  title_show.innerHTML = GlobalFunc.split_str(document.title_value,words_per_line);
  //显示悬停效果DIV
  title_show.style.display = 'block';

  //根据鼠标位置设定悬停效果DIV位置
  event = event || window.event;                            //鼠标、键盘事件
  var top_down = 50;                                        //下移15px避免遮盖当前标签
  //最左值为当前鼠标位置 与 body宽度减去悬停效果DIV宽度的最小值,否则将右端导致遮盖
  var left = Math.min(event.clientX,document.body.clientWidth-title_show.clientWidth);
  title_show.style.left = left+"px";            //设置title_show在页面中的X轴位置。
  title_show.style.top = (event.clientY - top_down)+"px";    //设置title_show在页面中的Y轴位置。
  // title_show.style.top = top_down+"%";    //设置title_show在页面中的Y轴位置。
}
/**
 * 鼠标离开隐藏TITLE
 * @params    obj        当前悬停的标签
 *
 */
GlobalFunc.titleMouseOut= function titleMouseOut(event) {
  var title_show = document.getElementById("title_show");
  //不存在悬停效果,直接返回
  if(title_show == null) return false;
  //存在悬停效果,恢复原TITLE
  event.target.title = document.title_value;
  //隐藏悬停效果DIV
  title_show.style.display = "none";
}
/**
 * className 类名
 * tagname HTML标签名,如div,td,ul等
 * @return Array 所有class对应标签对象组成的数组
 * @example
 <div class="abc">abc</div>
 var abc = getClass('abc');
 for(i=0;i<abc.length;i++){
     abc[i].style.backgroundColor='red';
 }
 */
GlobalFunc.getClass= function getClass(className,tagname) {
  //tagname默认值为'*',不能直接写成默认参数方式getClass(className,tagname='*'),否则IE下报错
  if(typeof tagname == 'undefined') tagname = '*';
  if(typeof(getElementsByClassName) == 'function') {
    return getElementsByClassName(className);
  }else {
    var tagname = document.getElementsByTagName(tagname);
    var tagnameAll = [];
    for(var i = 0; i < tagname.length; i++) {
      if(tagname[i].className == className) {
        tagnameAll[tagnameAll.length] = tagname[i];
      }
    }
    return tagnameAll;
  }
}
/**
 * JS字符切割函数
 * @params     string                原字符串
 * @params    words_per_line        每行显示的字符数
 */
GlobalFunc.split_str= function split_str(string,words_per_line) {
  //空串,直接返回
  if(typeof string == 'undefined' || string.length == 0) return '';
  //单行字数未设定,非数值,则取默认值50
  if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
    words_per_line = 50;
  }
  //格式化成整形值
  words_per_line = parseInt(words_per_line);
  //取出i=0时的字,避免for循环里换行时多次判断i是否为0
  var output_string = string.substring(0,1);
  //循环分隔字符串
  for(var i=1;i<string.length;i++) {
    //如果当前字符是每行显示的字符数的倍数,输出换行
    // if(i%words_per_line == 0) {
    //     output_string += "<br/>";
    // }
    //每次拼入一个字符
    output_string += string.substring(i,i+1);
  }
  return output_string;
}

/**
 * 悬停事件绑定
 * @params    objs        所有需要绑定事件的Element
 *
 */
GlobalFunc.attachEvent= function attachEvent(objs,words_per_line){
  if(typeof objs != 'object') return false;
  //单行字数未设定,非数值,则取默认值50
  if(typeof words_per_line == 'undefined' || isNaN(words_per_line)){
    words_per_line = 10;
  }
  for(let i=0;i<objs.length;i++){
    objs[i].onmouseover = function(event){
      GlobalFunc.titleMouseOver(this,event,words_per_line);
    }
    objs[i].onmouseout = function(event){
      GlobalFunc.titleMouseOut(this);
    }
  }
}


使用:

    <div title="实现悬停实现悬停的TITLE"
         @mouseenter="GlobalFunc.titleMouseOver($event,15)"
         @mouseleave="GlobalFunc.titleMouseOut($event)"
    >鼠标悬停[直接调用函数版本,设定行字数]</div>
   <style>
    .title_focus_toolTip {
        z-index: 7;
        position: absolute;
        display: none;
        min-width: 200px;
        min-height: 50px;max-width: 200px;word-break: normal;
        border-style: solid;
        transition: left 0.4s cubic-bezier(0.23, 1, 0.32, 1),
        top 0.4s cubic-bezier(0.23, 1, 0.32, 1);
        background-color: rgba(50, 50, 50, 0.701961);
        border-width: 0px;
        border-color: #333333;
        border-radius: 4px;
        color: #ffffff;
        font-style: normal;
        font-variant: normal;
        font-weight: normal;
        font-stretch: normal;
        font-size: 14px;
        font-family: "Microsoft YaHei";
        line-height: 21px;
        padding: 10px 10px;
        z-index: 9999;
        pointer-events: none;
    }
    
</style>

效果:
在这里插入图片描述

二、方案2 我们也可以写简单一点

定义一个div,作为弹框,需要显示传入内容即可

  <div id="focus_toolTip" class="special_focus_toolTip" v-html="dispatchSystemAddressBookTopbody">
                            </div>
 .special_focus_toolTip {
        z-index: 7;
        position: absolute;
        display: none;
        /*min-width: 200px;*/
        min-height: 80px;
        max-width: 200px;
        word-break: normal;
        border-style: solid;
        transition: left 0.4s cubic-bezier(0.23, 1, 0.32, 1),
        top 0.4s cubic-bezier(0.23, 1, 0.32, 1);
        background-color: rgba(50, 50, 50, 0.701961);
        border-width: 0px;
        border-color: #333333;
        border-radius: 4px;
        color: #ffffff;
        font-style: normal;
        font-variant: normal;
        font-weight: normal;
        font-stretch: normal;
        font-size: 14px;
        font-family: "Microsoft YaHei";
        line-height: 21px;
        padding: 10px 10px;
        z-index: 9999;
        pointer-events: none;
    }

鼠标事件

 <span  @mouseenter="GLOBALItemMouseover($event,'focus_toolTip',item.name) "
                                                          @mouseleave ="dispatchSystemAddressBookGLOBALIitemMouseout('focus_toolTip')" class="dispatchSystemAddressBookItemText" >{{item.name}}</span>

全局自定义事件

   GLOBALItemMouseover(e,id,name){
        this.dispatchSystemAddressBookTopbody = this.GlobalFunc.GLOBALItemMouseover(e,id,name);
      },
      dispatchSystemAddressBookGLOBALIitemMouseout(id){
      this.GlobalFunc.GLOBALIitemMouseout(id);
      },
/**
 * 自定义鼠标悬浮弹框
 */
GlobalFunc.GLOBALItemMouseover=(e,toolTipId,name)=> {
  var focusTooltip = $("#"+toolTipId);
  focusTooltip.css("top", e.clientY -140+ "px");
  focusTooltip.css("left", e.clientX + "px");
  var headerHtml =
      "<div style='font-size:14px;color: #ffffff;font-weight: bold;font-family: MicrosoftYaHei;pointer-events: none'>" +
      name +
      "</div>";
  var effectHtml =
      "<div style='font-size:12px;margin-top:5px;pointer-events: none'>" + "</div>";
  let params = headerHtml + effectHtml;
  GlobalFuncDasConstants.setToolTopbody(name);
  console.log(GlobalFuncDasConstants.aaoolTopbody);
  GlobalFunc.ttoolTopbody = params;
  focusTooltip.css("display", "block");
  return params;
}
/**
 * 自定义鼠标悬浮弹框关闭
 */
GlobalFunc.GLOBALIitemMouseout=function GLOBALIitemMouseout(toolTipId) {
  var focusTooltip = $("#"+toolTipId);
  focusTooltip.css("display", "none");
}
/**
 * 自定义鼠标移动
 */
GlobalFunc.GLOBALItemMousemove=function  GLOBALItemMousemove(e,toolTipId,name) {
  var self = this;
  var focusTooltip = $("#"+toolTipId);
  focusTooltip.css("top", e.clientY - 80 + "px");
  focusTooltip.css("left", e.clientX + 100 + "px");
  var headerHtml =
      "<div style='font-size:12px;color: #fec443;font-weight: bold;font-family: MicrosoftYaHei;'>" +
      name +
      "</div>";
  var effectHtml =
      "<div style='font-size:12px;margin-top:5px;'>" + "</div>";
  let params = headerHtml + effectHtml;
  return params;
}

自定义效果

在这里插入图片描述

三、这里存在闪动问题

照成的原因是:悬停上去信息框div盖住了span标签,mouseover事件失效,mouseout事件生效,信息框消失。
信息框消失后鼠标又正好悬停在span标签上,mouseover事件生效,mouseout事件失效,信息框显示。。。一直无限循环就会看到一直闪烁的现象。

解决办法 :在你需要显示的信息框上加上pointer-events: none


http://www.niftyadmin.cn/n/4204503.html

相关文章

java 判断以中文开始_java判断是否是中文字符

public class StringUtil {/*** 判断是否为中文字符* param c* return*/private static boolean isChinese(char c) {// GENERAL_PUNCTUATION 判断中文的“号// CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号// HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的&#xff0c;号Characte…

百度-相信中国-电子书-下载

/Files/dayouluo/相信中国.rar

JS之给元素添加类的方法

原生js中添加类的方法 //1.为 <div> 元素添加一个类: document.getElementById("div").classList.add("类名");//2.为 <div> 元素添加多个类: document.getElementById("div").classList.add("类名1","类名2",…

java中io操作详解_Java语言中的IO系统详解

Java语言中的IO系统Java的核心库java.io提供了全面的IO接口&#xff0c;包括&#xff1a;文件读写&#xff0c;标准设备输出等等。Java中IO是以流为基础进行输入输出的&#xff0c;所有数据被串行化写入输出流&#xff0c;或者从输入流读入。在具体使用中很多初学者对Java.io包…

工程师侵入北京移动数据库 获利370余万元

程稚瀚称&#xff0c;侵入北京移动数据库是因为他对移动的“霸王条款”不满。  在5个月的时间里&#xff0c;软件研发工程师程稚瀚利用互联网4次侵入北京移动充值中心数据库&#xff0c;盗取充值卡密码并通过淘宝网出售&#xff0c;共获利370余万元。昨天&#xff0c;这起全国…

mysql-8.0.12版本忘记root密码解决方法(重置root密码)

解决方法&#xff1a; 1.以管理员身份运行命令行&#xff0c;输入命令&#xff1a;net stop mysql&#xff0c;以停止MySQL服务 2.设置跳过验证&#xff0c;进入到mysql安装目录下的bin路径&#xff0c;在mysql/bin/目录下输入命令&#xff1a;“mysqld --shared-memory --sk…

java界面控件_java-图形界面(控件)

java-图形界面(控件)java-图形界面(控件)记录学习过程import javax.swing.*;import java.awt.*;public class LoginPanel extends JPanel {public static final int LEFT_PADDING50;public static final int RIGHT_PADDING50;public static final int TOP_PADDING20;public sta…

adb命令汇总

adbtools 看unity在安卓设备上的运行情况&#xff0c;看logcat 还有看内存占用情况。 这是我在短暂的安卓开发过程&#xff0c;汇总的知识点&#xff0c;如果好用请留言告诉。 使用adb命令管理设备 adb devices 显示连接到计算机的设备 adb get-serialno…