Onclick CSS button effect

前端 未结 4 2256
梦毁少年i
梦毁少年i 2021-02-05 12:56

I\'m creating a CSS button and I\'m trying to make an onclick effect: when the user clicks on the button it would push down the button text by 1px. My problem here is that it\'s

4条回答
  •  南旧
    南旧 (楼主)
    2021-02-05 13:35

    JS provides the tools to do this the right way. Try the demo snippet.

    var doc = document;
    var buttons = doc.getElementsByTagName('button');
    var button = buttons[0];
    
    button.addEventListener("mouseover", function(){
      this.classList.add('mouse-over');
    });
    
    button.addEventListener("mouseout", function(){
      this.classList.remove('mouse-over');
    });
    
    button.addEventListener("mousedown", function(){
      this.classList.add('mouse-down');
    });
    
    button.addEventListener("mouseup", function(){
      this.classList.remove('mouse-down');
      alert('Button Clicked!');
    });
    
    //this is unrelated to button styling.  It centers the button.
    var box = doc.getElementById('box');
    var boxHeight = window.innerHeight;
    box.style.height = boxHeight + 'px'; 
    button{
      text-transform: uppercase;
      background-color:rgba(66, 66, 66,0.3);
      border:none;
      font-size:4em;
      color:white;
      -webkit-box-shadow: 0px 10px 5px -4px rgba(0,0,0,0.33);
      -moz-box-shadow: 0px 10px 5px -4px rgba(0,0,0,0.33);
      box-shadow: 0px 10px 5px -4px rgba(0,0,0,0.33);
    }
    button:focus {
      outline:0;
    }
    .mouse-over{
      background-color:rgba(66, 66, 66,0.34);
    }
    .mouse-down{
      -webkit-box-shadow: 0px 6px 5px -4px rgba(0,0,0,0.52);
      -moz-box-shadow: 0px 6px 5px -4px rgba(0,0,0,0.52);
      box-shadow: 0px 6px 5px -4px rgba(0,0,0,0.52);                 
    }
    
    /* unrelated to button styling */
    #box {
      display: flex;
      flex-flow: row nowrap ;
      justify-content: center;
      align-content: center;
      align-items: center;
      width:100%;
    }
    
    button {
      order:1;
      flex: 0 1 auto;
      align-self: auto;
      min-width: 0;
      min-height: auto;
    }            
    
    
        
    
    
      
        
        
      
    
      
        

提交回复
热议问题