dr897777 2018-10-19 03:30
浏览 50
已采纳

制作更大并将按钮恢复为原始大小的Javascript

I have a project to make 2 buttons that containing value before and after test score.

The student is 50 and every student have 2 buttons that containing their previous and present test score in some subject. the button is placed in 2 columns Before and After, sorted by highest score and resize them depending on their score. So we don't know how to identify which was the pair of those buttons.

So I decide to make if that "id" of pairing button is same eg: id="Score1" and id="Score1_a"

Progress now, I make the pairing button in "Before" and "After" Columns change their color to Red if we hover mouse on one of them.

But it's still not enough.. So I use script to make the buttons bigger when we hover on it, but I don't know how to Revert it back to original size. Can you guys help me?

This is my script:

 <script>
  function mouseover(obj) {
 var id2=obj.id+"_a";
document.getElementById(obj.id).style.background = "magenta";
document.getElementById(obj.id).style.width = "100px";
document.getElementById(obj.id).style.height = "100px";
document.getElementById(id2).style.background = "magenta";
    }
  function mouseout(obj) {
 var id2=obj.id+"_a";
document.getElementById(obj.id).style.background = "deepskyblue";
document.getElementById(id2).style.background = "deepskyblue";
document.getElementById(obj.id).style.offsetwidth;
document.getElementById(obj.id).style.offsetheight;
   }
</script>

  <button id="score1" style='width:70px; height:70px;'>70</button>
  <button id="score1_a" style='width:65px; height:65px;'>65</button>
   <button id="score2" style='width:25px; height:25px;'>25</button>
   <button id="score2_a" style='width:50px; height:50px;'>50</button> 

Thanks.

  • 写回答

1条回答 默认 最新

  • doudiejian5827 2018-10-19 04:12
    关注

    Not sure what is that you are trying to achieve but based on my understanding i am retaining its original dimensions on mouse out. There are multiple ways we can achieve this in more efficient way but make your code working you you will need to track the original height width and also its pair for each button. So solution can be,

    function mouseover(obj, pairId) {
      
        var id2=pairId;
    
        document.getElementById(obj.id).style.background = "magenta";
        document.getElementById(obj.id).style.width = "100px";
        document.getElementById(obj.id).style.height = "100px";
        document.getElementById(id2).style.background = "magenta";
    }
    function mouseout(obj, pairId, actualDim) {
    
        var id2=pairId;
    
        document.getElementById(obj.id).style.background = "deepskyblue";
        document.getElementById(id2).style.background = "deepskyblue";
        document.getElementById(obj.id).style.width = actualDim.w;
        document.getElementById(obj.id).style.height = actualDim.h;
    }
     <button id="score1" style='width:70px; height:70px;' onmouseover="mouseover(this, 'score1_a')" onmouseout="mouseout(this, 'score1_a', {w:'70px', h:'70px'})">70</button>
      <button id="score1_a" style='width:65px; height:65px;' onmouseover="mouseover(this, 'score1')" onmouseout="mouseout(this, 'score1', {w:'65px', h:'65px'})">65</button>
      <button id="score2" style='width:25px; height:25px;' onmouseover="mouseover(this, 'score1')" onmouseout="mouseout(this, 'score1', {w:'25px', h:'25px'})">25</button>
      <button id="score2_a" style='width:50px; height:50px;' onmouseover="mouseover(this, 'score1')" onmouseout="mouseout(this, 'score1', {w:'50px', h:'50px'})">50</button> 

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

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘