dongliao1949 2017-02-16 19:29
浏览 403
已采纳

HTML / JavaScript悬停时展开按钮/ div

I know the onmouseover and onmouseout so that they can run a function when hovered over on when not being hovered over but I don't know how to make them expand. I have made dropdown boxes, but I can't get them to expand. EX:

123 [Not Hovering]
123456789123 [During Hover]

I've Tried This:

<!DOCTYPE html>
    <head>
    </head>
    <body>
        <p id="exp" onmouseover="over()" onmouseout="out()" style="background- color: #FFD700;width: 100px;height: 250px;">
            Expand Me!
        </p>
        <button onclick="run()">Run</button>
        <script type="text/javascript">
            function over() {
                document.getElementById("exp").style.width = "250px";
            }
            function out() {
                document.getElementById("exp").style.width = "100px";
            }
        </script>
        <div id="x" style="background-color: #0FF">&emsp;</div>
    </body>
</html>

but I want it to come out and go back in slowly not instantly, and I'm sure there is a better way to do this because I don't want to go pixel by pixel to get it to go slower because that would lagg the webpage

  • 写回答

1条回答 默认 最新

  • duanfu6160 2017-02-16 20:03
    关注

    I think a CSS transition would be best for this, no JS needed.

    div {
      width: 25px;
      overflow: hidden;
      transition: width 1s;
    }
    
    div:hover {
      width: 100%;
    }
    <div>123456789123</div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀