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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法