添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

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

  • 使用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">
          :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 = '' //绝对定位 /* +'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

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;&lt;html&gt;&lt;head&gt;&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;&lt;title&gt;JS控制TITLE悬停效果&lt;/title&gt;&lt;sc
之前的写法: <tab-bar-item path="/category"> <img slot="item-icon" src="~assets/img/tabbar/category.svg" alt=""> <img slot="item-icon-active" src="~assets/img/tabbar/category_active.svg" alt=""> <div slot="item-text">分类
点击的时候第一层出现了但是点击指南或者资源的时候,他就报错Cannot read property 'getElementsByClassName' of undefined &lt;el-cascader                       :options="options2"                       @change="handleItemChange"
在很多时候,我们页面上其实有时候展示不出来很多很多文字的,这个时候我们就不得不对这个文字进行处理,但是我们鼠标放到文字上时,还想展示所有的文字,这种方式其实有2种 第一种是运用element ui的一个组件Tooltip 文字提示,常常用于鼠标hover时的提示信息 话不多说上代码 <el-tooltip class="item" effect="dark" content="Top Left 提示文字" placement="top"> <el-button>上边&
document.getElementById("id名") document.getElementByTagName("标签名") document.getElementByClassName("类名") 改变HTML内容 document.getElementById("i...
回答: 在Vue中,可以使用:title指令来动态设置元素的title属性。这个指令可以用于任何元素,不仅仅是页面的title。例如,你可以在一个组件中使用:title指令来设置一个按钮的title属性。这样,当鼠标悬停在按钮上时,会显示指定的title文本。另外,你还可以使用Vue Router来根据不同的路由设置不同的页面title。你可以在路由配置中使用meta字段来设置每个路由的title,然后在main.js文件中使用router.beforeEach钩子函数来监听路由变化,并根据当前路由的meta字段来动态修改页面的title。这样,当用户切换到不同的路由时,页面的title会自动更新。[1]