自动匹配显示位置的提示框--Auto_Tooltip

本文介绍了作者开发的一款简单易用、兼容性好且能自动匹配显示位置的提示框插件。该插件具备无兼容性问题、自动定位、轻量级等特点,并提供了示例和参数详解。设计思路包括8个显示方向,通过判断元素与窗口的位置关系确定显示位置。核心JS代码和CSS代码简洁,鼓励读者根据需求进行个性化扩展。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近工作中需要用到提示框(ToolTip) 在网上找了很多插件 不是兼容性不好就是不能自动匹配显示位置等问题。遂自己开发了一个简单易用的提示框。

这是一个简单的提示框插件 欢迎大神们指导。也欢迎童鞋们丰富出更加炫酷的样式更加牛叉的功能!!!

一 、特点:
1、不存在兼容性问题 能兼容最低版本的IE。
2、自动匹配显示位置。
3、轻量级,简单易用。
示例:
<a href="#none" onclick="Tooltip(this,'this is a toolTip',300,100)">rightBottom</a>

参数详解:
this–>当前元素 ‘this is toolTip’–>提示框的显示内容(string类型)
300–>提示框的宽度(我这里设置的最大宽度为200 参数大于200时自动转为200)
100–>提示框的高度(最小为高度为40)

二 、此插件的设计思路:
提示框的显示方向一共8个(top、left、bottom、right 、leftTop、rightTop、leftBottom、rightBottom)
三 、怎么判断具体显示在哪个方向:
例:top
if(topDis >= height && leftDis >= tipWidth/2 && rightDis >= tipWidth/2 ){ //在上方显示
console.log("top");
showTop(current,width,height,content);
}

如果 当前元素的距窗口上方的高度(topDis)大于提示框的高度(height) && 距离窗口左边的距离(leftDis)大于提示框宽度的一半(tipWidth/2) && 距离窗口右边的距离(rightDis)大于提示框宽度的一半(tipWidth/2 ) 就显示在上方。
其他方向请参考此例自行理解。

四 、核心js代码:

function showTop (current,width,height,content){
    $(".toolTip").toggle().css({"width":width,"height":height,     // 这里一定要先toggle再给定义样式 不然提示框第一次出现的时候没有宽高
        "left": $(current).offset().left - ($(".toolTip").width()/2 - $(current).width()/2),
        "top": $(current).offset().top - height - 2
    });
    tipContent (content)
};
function showBottom (current,width,height,content){
    $(".toolTip").toggle().css({"width":width,"height":height,
        "left": $(current).offset().left - ($(".toolTip").width()/2 - $(current).width()/2),
        "top": $(current).offset().top + $(current).height() + 2
    });
    tipContent (content)
};
function showLeft (current,width,height,content){
    $(".toolTip").toggle().css({"width":width,"height":height,
        "left": $(current).offset().left - width - 6,
        "top": $(current).offset().top - height/2 + $(current).height() / 2
    });
    tipContent (content)
};
function showRight (current,width,height,content){
    $(".toolTip").toggle().css({"width":width,"height":height,
        "left": $(current).offset().left + $(current).width() + 5,
        "top": $(current).offset().top - height/2 + $(current).height() / 2
    });
    tipContent (content)
};
function rightTop (current,width,height,content){
    $(".toolTip").toggle().css({"width":width,"height":height,
        "left": $(current).offset().left + $(current).width(),
        "top": $(current).offset().top - height
    });
    tipContent (content)
};
function leftTop (current,width,height,content){
    $(".toolTip").toggle().css({"width":width,"height":height,
        "left": $(current).offset().left - $(".toolTip").width(),
        "top": $(current).offset().top - height
    });
    tipContent (content)
};
function leftBottom (current,width,height,content){
    $(".toolTip").toggle().css({"width":width,"height":height,
        "left": $(current).offset().left - $(".toolTip").width(),
        "top": $(current).offset().top + $(current).height()
    });
    tipContent (content)
};
function rightBottom (current,width,height,content){
    $(".toolTip").toggle().css({"width":width,"height":height,
        "left": $(current).offset().left + $(current).width(),
        "top": $(current).offset().top + $(current).height()
    });
    tipContent (content)
}
function tipContent (content){
    //先清空再赋值
    $(".tipContent").html("");
    $(".tipContent").html(content);
};

function Tooltip(current,content,width,height){
    if(width > 200){
        width = 200;
        alert("最大宽度为200,已自动修改为200");
    }
    if(height < 40){
        height = 40;
        alert("最小高度为40,已自动修改为40");
    }
    // 首先给提示框的宽高赋值 方便以后调用
    $(".toolTip").css({"width":width +'px',"height":height +'px'});
    // 计算当前元素相对于窗口 上下左右的距离
    var leftDis = $(current).offset().left - $(document).scrollLeft();
    var rightDis = $(window).width() - leftDis - $(current).width();
    var topDis = $(current).offset().top - $(document).scrollTop();
    var bottomDis = $(window).height() - topDis - $(current).height();

    var tipWidth = parseInt($(".toolTip").css("width"));
    var tipHeight = parseInt($(".toolTip").css("height"));

    if(topDis >= height && leftDis >= tipWidth/2 && rightDis >= tipWidth/2 ){                   //在上方显示
        console.log("top");
        showTop(current,width,height,content);
    }else if(bottomDis >= height && leftDis >= tipWidth/2 && rightDis >= tipWidth/2){           // 在下方显示
        console.log("bottom");
        showBottom(current,width,height,content);
    }else if(leftDis >= width && topDis >= tipHeight/2 && bottomDis >= tipHeight/2 ){           // 在左边显示
        console.log("left");
        showLeft(current,width,height,content);
    }else if(rightDis >= width && topDis >= tipHeight/2 && bottomDis >= tipHeight/2 ){          // 在右边显示
        console.log("right");
        showRight(current,width,height,content);
    }else if(topDis >= height && leftDis < tipWidth/2 && rightDis >= tipWidth){                 // 在右上方显示
        console.log("rightTop");
        rightTop(current,width,height,content);
    }else if(topDis >= height && leftDis >= tipWidth && rightDis < tipWidth/2){                 // 在左上方显示
        console.log("leftTop");
        leftTop(current,width,height,content);
    }else if(bottomDis >= height && leftDis < tipWidth/2 && rightDis >= tipWidth){              // 在右下方显示
        console.log("rightBottom");
        rightBottom(current,width,height,content);
    }else if(bottomDis >= height && leftDis >= tipWidth && rightDis < tipWidth/2){              // 在左下方显示
        console.log("leftBottom");
        leftBottom(current,width,height,content);
    }else {                                                                                     // 默认在上方显示
        console.log("top2");
        showTop(current,width,height,content);
    }
}
$(function (){
    $(".close").click(function (){
        $(".toolTip").hide();
    });
});

这里本人根据工作的需求只做了现在的功能 同学们可以根据自己的需求自行丰富。

css代码如下 本人写的比较简单,欢迎同学们自定出更加炫酷的样式。

.toolTip {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 9999;
    display: none;
    overflow: auto;
    min-height: 40px;
    max-width: 200px;
    padding: 1px;
    text-align: left;
    white-space: normal;
    background-color: #fff;
    background-clip: padding-box;
    color:#666;
    border: 1px solid #ccc;
    font-family:"Microsoft YaHei",Tahoma,Arial,Helvetica;
    border-radius: 6px;
    -webkit-box-shadow: 0 5px 10px rgba(0, 0, 0, .2);
    box-shadow: 0 5px 10px rgba(0, 0, 0, .2)
}
.close {
    position: absolute;
    right: 10px;
    top: 0px;
    cursor: pointer;
    font-size: 20px;
    color: #000;
    font-weight: bold;
}
.tipContent {
    overflow: auto;
    margin-top: 20px;
    margin-left: 10px;
    margin-right: 10px;
}

这是一个简单的提示框插件 欢迎大神们指导。也欢迎童鞋们丰富出更加炫酷的样式更加牛叉的功能!!!

<script lang="ts"> import { ref, onMounted } from 'vue' import { ElMessage, ElMessageBox } from 'element-plus' import { useRouter } from 'vue-router' import request from '@/utils/request' import type { ResultModel } from '@/api/stu' // 定义登录请求数据类型 interface LoginData { username: string password: string } //修改密码 export const updatePasswordApi = (data: LoginData) => request.put<any, ResultModel>('/emps/update', data) //修改员工信息 let router = useRouter() const loginName = ref('') //定义钩子函数, 获取登录用户名 onMounted(() => { //获取登录用户名 const token = localStorage.getItem('token') || '{}' let loginUser = JSON.parse(token) console.log(loginUser) if (loginUser) { loginName.value = loginUser.name } }) const logout = () => { //弹出确认框, 如果确认, 则退出登录, 跳转到登录页面 ElMessageBox.confirm('确认退出登录吗?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', }).then(() => { //确认, 则清空登录信息 ElMessage.success('退出登录成功') localStorage.removeItem('loginUser') router.push('/login') //跳转到登录页面 }) } </script> <template> <div class="common-layout"> <el-container class="layout-container"> <!-- 头部 --> <el-header class="header"> <span class="title">AI教学管理系统</span> <div class="tools"> <a href=""> <el-icon><EditPen /></el-icon> 修改密码 </a> <a href="javascript:void(0)" @click="logout"> <el-icon><SwitchButton /></el-icon> 退出登录 【{{ loginName }}】 </a> </div> </el-header> <el-container class="main-container"> <!-- 侧边栏 --> <el-aside class="aside"> <!-- 使用 el-scrollbar 包裹菜单 --> <el-scrollbar class="menu-scrollbar"> <el-menu router default-active="1" class="side-menu" background-color="#2c3e50" text-color="#ecf0f1" active-text-color="#f39c12" > <!-- 首页 --> <el-menu-item index="/index"> <el-icon><Menu /></el-icon> <span>首页</span> </el-menu-item> <!-- 班级学员管理子菜单 --> <el-sub-menu index="2" class="menu-group"> <template #title> <el-icon><Document /></el-icon> <span>班级学员管理</span> </template> <el-menu-item index="/stu" ><el-icon><UserFilled /></el-icon>学员管理</el-menu-item > <el-menu-item index="/clazz" ><el-icon><HomeFilled /></el-icon>班级管理</el-menu-item > </el-sub-menu> <!-- 部门员工管理子菜单 --> <el-sub-menu index="3" class="menu-group"> <template #title> <el-icon><Avatar /></el-icon> <span>部门管理</span> </template> <el-menu-item index="/emp" ><el-icon><Avatar /></el-icon>员工管理</el-menu-item > <el-menu-item index="/dept" ><el-icon><HelpFilled /></el-icon>部门管理</el-menu-item > </el-sub-menu> <!-- 新增的统计管理子菜单 --> <el-sub-menu index="4" class="menu-group"> <template #title> <el-icon><PieChart /></el-icon> <span>数据统计管理</span> </template> <el-menu-item index="/report/emp" ><el-icon><InfoFilled /></el-icon>员工信息统计</el-menu-item > <el-menu-item index="/report/stu" ><el-icon><Share /></el-icon>学员信息统计</el-menu-item > <el-menu-item index="/log" ><el-icon><clock /></el-icon>日志信息统计</el-menu-item > </el-sub-menu> </el-menu> </el-scrollbar> </el-aside> <!-- 主内容区 --> <el-main class="main-content"> <router-view></router-view> </el-main> </el-container> </el-container> </div> </template> <style scoped> /* 布局基础样式 */ .common-layout { height: 100vh; display: flex; flex-direction: column; } .layout-container { height: 100%; display: flex; flex-direction: column; } /* 头部样式 */ .header { background-color: #2c3e50; color: white; display: flex; justify-content: space-between; align-items: center; padding: 0 20px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); z-index: 10; } .title { font-size: 1.5rem; font-weight: bold; } .tools a { color: #ecf0f1; text-decoration: none; margin-left: 20px; display: inline-flex; align-items: center; transition: background-color 0.3s ease; border-radius: 4px; padding: 6px 10px; } .tools a:hover { background-color: #34495e; } /* 侧边栏样式 */ .aside { background-color: #2c3e50; color: #ecf0f1; height: 100%; padding-bottom: 20px; } /* 自定义滚动条样式 */ .menu-scrollbar { height: calc(100vh - 60px); /* 减去 header 高度 */ } .menu-scrollbar .el-scrollbar__bar.is-vertical { width: 6px; right: 2px; border-radius: 4px; background-color: rgba(255, 255, 255, 0.1); } .menu-scrollbar .el-scrollbar__thumb { background-color: rgba(255, 255, 255, 0.3); border-radius: 4px; transition: background-color 0.3s ease; } .menu-scrollbar .el-scrollbar__thumb:hover { background-color: rgba(255, 255, 255, 0.5); } /* 菜单样式 */ .side-menu { background-color: transparent; border-right: none; padding: 10px 0; } /* 菜单项通用样式 */ .side-menu .el-menu-item, .side-menu .el-sub-menu .el-sub-menu__title { margin: 6px 0; padding: 10px 20px; font-size: 14px; color: #ecf0f1; border-radius: 6px; transition: all 0.3s ease; } /* 悬停样式 */ .side-menu .el-menu-item:hover, .side-menu .el-sub-menu .el-sub-menu__title:hover { background-color: #34495e; } /* 激活项样式 */ .side-menu .el-menu-item.is-active { background-color: #34495e; color: #f39c12 !important; font-weight: bold; } /* 子菜单项样式 */ .menu-group .el-menu-item { font-size: 13px; padding-left: 40px !important; color: #dcdcdc; transition: all 0.3s ease; } .menu-group .el-menu-item:hover { background-color: #3a5267; } .menu-group .el-menu-item.is-active { background-color: #3a5267; color: #f1c40f; } /* 主内容区样式 */ .main-content { background-color: #f9f9f9; padding: 20px; flex: 1; overflow-y: auto; } /* 响应式设计 */ @media (max-width: 768px) { .tools a { margin-left: 10px; font-size: 0.9rem; padding: 4px 8px; } .title { font-size: 1.2rem; } .menu-group .el-menu-item { font-size: 12px; padding-left: 25px !important; } } </style> 优化一下代码逻辑,以及头部效果 ,有个修改密码的api完善一下修改密码功能
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值