利用JS实现鼠标移入展示用于介绍的文本的样式

介绍:当鼠标移入目标区域后,会展示出对目标区域内容的文本(利用html的标签展示文本)

html与css部分

    <div style="width: 25%;border: solid 1px black; " id="lol"><img style="height: 290px;width: 100%;" src="../素材库/英雄联盟.webp" alt=""></div>  为了方便理解,这里直接在div标签中添加style样式。同时给四组标签分别命名id名
    <div style="width: 25%;border: solid 1px black; " id="dot"><img style="height: 290px;width: 100%;" src="../素材库/DOTA2.webp" alt=""></div>   
    <div style="width: 25%;border: solid 1px black; " id="her3"><img style="height: 290px;width: 100%;" src="../素材库/300英雄.webp" alt=""></div>
    <div style="width: 25%;border: solid 1px black; " id="stomszdcx"><img style="height: 290px;width: 100%;" src="../素材库/风暴英雄.webp" alt=""></div>
       
       //    上面是四个目标图片及其基本注释

       //    下面是各个板块的注释,初始时只有第一块显示。之后只有当鼠标指在对应的图片时,才会
       
        <span style="background-color: rgba(255, 255, 255, 0);width: 500px; height: 1000px;" id="la">
        <h1>英雄联盟-LOL</h1>
        <p>这是英雄联盟,这是英雄联盟这是英雄联盟。这是英雄联盟这是英雄联盟这是英雄联盟,这是英雄联盟。</p>
    </span>

    <span style="background-color: rgba(255, 255, 255, 0);width: 500px; height: 1000px;display: none;" id="da">
        <h1>DOTA2</h1>
        <p>这是DOTA2,这是DOTA2这是DOTA2这是DOTA2。这是DOTA2这是DOTA2,这是DOTA2这是DOTA2;这是DOTA2这是DOTA2这是DOTA2(这是DOTA2)这是DOTA2这是DOTA2这是DOTA2.</p>
    </span>

    <span style="background-color: rgba(255, 255, 255, 0);width: 500px; height: 1000px;display: none;" id="he">
        <h1>300HERO</h1>
        <p>这是300HERO300HERO,这是300HERO这是300HERO这是300HERO。这是300HERO“这是300HERO这是300HERO!”这是300HERO这是300HERO这是300HERO这是300HERO;这是300HERO这是300HERO;这是300HERO这是300HERO这是300HERO。</p>
    </span>

    <span style="background-color: rgba(255, 255, 255, 0);width: 500px; height: 1000px;display: none;" id="st">
        <h1>风暴英雄</h1>
        <p>这是风暴英雄这是风暴英雄这是风暴英雄,这是风暴英雄这是风暴英雄这是风暴英雄这是风暴英雄。这是风暴英雄:这是风暴英雄这是风暴英雄这是风暴英雄这是风暴英雄;这是风暴英雄这是风暴英雄!这是风暴英雄这是风暴英雄(这是风暴英雄)</p>
    </span>

        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Poppins',sans-serif;
        }
        
        div{
            float: left;
        }

上面是对页面进行基础设置的CSS语言


JS部分

        document.querySelector('#lol').addEventListener('mouseenter',laenteraHandler) //为第一个id名为div的标签设置鼠标进入的事件
        document.querySelector('#lol').addEventListener('mouseleave',laleaveaHandler) //为第一个id名为div的标签设置鼠标离开的事件
        let la = document.getElementById('la'); //从body中获取id值la
        function laenteraHandler(e) {           //为la的鼠标进入事件设置函数 
            la.style.display = 'inline'                     
            console.log(123)
        }
        function laleaveaHandler(e){
            la.style.display = 'none'           //为la的鼠标进入事件设置函数
            console.log(456)
        }

//下面每一块的原理都是相同的,与上面一致
        document.querySelector('#dot').addEventListener('mouseenter',daenteraHandler)
        document.querySelector('#dot').addEventListener('mouseleave',daleaveaHandler)
        let da = document.getElementById('da');
        function daenteraHandler(e) {
            da.style.display = 'inline'
            console.log(123)
        }
        function daleaveaHandler(e){
            da.style.display = 'none'
            console.log(456)
        }


        document.querySelector('#her3').addEventListener('mouseenter',heenteraHandler)
        document.querySelector('#her3').addEventListener('mouseleave',heleaveaHandler)
        let he = document.getElementById('he');
        function heenteraHandler(e) {
            he.style.display = 'inline'
            console.log(123)
        }
        function heleaveaHandler(e){
            he.style.display = 'none'
            console.log(456)
        }


        document.querySelector('#stomszdcx').addEventListener('mouseenter',stenteraHandler)
        document.querySelector('#stomszdcx').addEventListener('mouseleave',stleaveaHandler)
        let st = document.getElementById('st');
        function stenteraHandler(e) {
            st.style.display = 'inline'
            console.log(123)
        }
        function stleaveaHandler(e){
            st.style.display = 'none'
            console.log(456)
        }

效果如下

Rec 0004

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用D3的鼠标事件和SVG文本元素来实现这个功能。以下是一个基本的实现方法: 1. 创建一个SVG圆形元素和一个SVG文本元素,将文本元素的内容设置为需要显示的文本。 2. 使用D3的鼠标事件(mouseover和mouseout)来监听鼠标出圆形的事件。 3. 在鼠标圆形的事件处理程序中,将文本元素的样式(例如显示、颜色、位置等)进行修改,使其显示在圆形中。 4. 在鼠标出圆形的事件处理程序中,将文本元素恢复到初始状态(例如隐藏、颜色、位置等)。 以下是一个示例代码: ```javascript // 创建SVG元素 var svg = d3.select("body") .append("svg") .attr("width", 400) .attr("height", 400); // 创建圆形和文本元素 var circle = svg.append("circle") .attr("cx", 200) .attr("cy", 200) .attr("r", 50) .attr("fill", "blue"); var text = svg.append("text") .attr("x", 200) .attr("y", 200) .style("text-anchor", "middle") .style("dominant-baseline", "central") .style("fill", "white") .style("font-size", "20px") .text("Hello, World!"); // 监听鼠标出圆形的事件 circle.on("mouseover", function() { text.style("display", "block") .attr("x", 200) .attr("y", 200); }); circle.on("mouseout", function() { text.style("display", "none"); }); ``` 在这个示例中,当鼠标圆形时,文本元素将被设置为可见,并且在圆形中心位置。当鼠标出圆形时,文本元素将被隐藏。你可以根据自己的需要修改文本元素的样式和位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值