JS实现定时器+提示框

这次给大家带来JS实现定时器+提示框,JS实现定时器+提示框的注意事项有哪些,下面就是实战案例,一起来看一下。

业务场景:当鼠标移入某元素时,显示提示框进行介绍。当鼠标移除时,会自动消失。引入ToolTip.js和ToolTip.css

主方法:ToolTip.show(需要提示的元素id, 随意不重复即可, 要提示的html文本, 宽(可不指定), 高(可不指定));

ToolTip.show(obj, id, html, width, height);

效果如下:

1.显示文本:

JS实现定时器+提示框

2:显示图片

JS实现定时器+提示框

 3:显示网站

JS实现定时器+提示框

js代码:F:Html5PluginsToolTipjsToolTip.js    

  1. (function () { var ToolTip = {}; /** * 显示函数 */ ToolTip._showTip = function (parentId, childId, html, width, height) { var parent = document.getElementById(parentId)//要提示的元素 var child = document.getElementById(childId); if (child === null) {//创建 var toolTip = document.createElement("p"); toolTip.classList = "ui-tooltip-box"; toolTip.id = childId; toolTip.innerHTML = html; parent.appendChild(toolTip); toolTip.style.width = width ? width + "px" : "auto" toolTip.style.height = height ? height + "px" : "auto" //定位: toolTip.style.position = "absolute"; toolTip.style.display = "block"; var left = parent.offsetLeft; var top = parent.offsetTop; if (left + toolTip.offsetWidth > document.body.clientWidth) { left = document.body.clientWidth / 2; } toolTip.style.left = left + "px"; toolTip.style.top = top + 20 + "px"; parent.onmouseleave = function (ev) { setTimeout(function () { //延迟: document.getElementById(childId).style.display = "none";//隐藏 }, 300); } } else { //显示 document.getElementById(childId).style.display = "block"; } }, /** * 调用入口 */ ToolTip.show = function (parentId, childId, html, width, height) { var parent = document.getElementById(obj) parent.onmouseenter = function (ev) { ToolTip._showTip(parentId, childId, html, width, height) } } window.ToolTip = ToolTip;})();//为防止污染,将方法写在匿名函数中

登录后复制

html代码:F:Html5PluginsToolTipToolTip.html

  1. <span class="pun">提示框</span>

    唐诗

    背景图片

    Yi人诗社

    //调用方式 ToolTip.show("tooltip-text", "01", "唐诗泛指创作于唐朝的诗" + "。唐诗是中华民族最珍贵的文化遗产之一,是" + "中华文化宝库中的一颗明珠," + "同时也对世界上许多民族和国家的文化发展产生了很大影响," + "对于后人研究唐代的政治、民情、风俗、" + "文化等都有重要的参考意义和价值。",300,90); ToolTip.show("tooltip-photo", "02", "JS实现定时器+提示框",150,80); var html='' ToolTip.show("tooltip-poem", "03", html);

登录后复制

css代码:F:Html5PluginsToolTipToolTip.css

  1. body { font-size: 14px; line-height: 1.8; background-image: url("https://www.php.cn/faq/imgs/bg.jpg");}.ui-tooltip-demo { width: 500px; margin: 30px auto; padding: 20px 30px; background-color: rgba(100%, 100%, 100%, 0.4); border-radius: 10px; text-align: center; box-shadow: 2px 1px 0px 3px rgba(0, 0, 0, 0.2);}.ui-tooltip-demo .ui-tooltip { color: #03f; font-size: 18px; cursor: help;}.ui-tooltip-box { display: block; background: #fff; line-height: 1.6; border: 1px solid #6cf; color: #333; padding: 20px; font-size: 12px; border-radius: 5px; overflow: auto;}

登录后复制

相信看了本文案例你已经掌握了方法,更多精彩请关注【创想鸟】其它相关文章!

推荐阅读:

vue的单页应用前端路由使用详解

vue cli升级webapck4的使用方法

D3.js实现动态仪表盘案列

以上就是JS实现定时器+提示框的详细内容,更多请关注【创想鸟】其它相关文章!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

点点赞赏,手留余香

给TA打赏
共0人
还没有人赞赏,快来当第一个赞赏的人吧!
    编程技术

    Vue filter的使用详解

    2025-3-8 14:12:17

    编程技术

    vue cli升级webapck4的使用方法

    2025-3-8 14:12:26

    0 条回复 A文章作者 M管理员
    欢迎您,新朋友,感谢参与互动!
      暂无讨论,说说你的看法吧
    个人中心
    购物车
    优惠劵
    今日签到
    私信列表
    搜索