【问题标题】:Onclick CSS button effectOnclick CSS 按钮效果
【发布时间】:2014-02-14 05:37:36
【问题描述】:

我正在创建一个 CSS 按钮,并且正在尝试制作 onclick 效果:当用户单击按钮时,它会将按钮文本下推 1 像素。我的问题是它推动了按钮的整个底部。你会怎么做?

<div class="one">
    <p><a id="button" href=#>Button</a></p>
</div>

这是 CSS:

#button {
    display: block;
    width:150px;
    margin:10px auto;
    padding:7px 13px;
    text-align:center;
    background:#a2bb33;
    font-size:20px;
    font-family: 'Arial', sans-serif;
    color:#ffffff;
    white-space: nowrap;
    box-sizing: border-box;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;

}

#button:active {
    vertical-align: top;
    padding-top: 8px;
}

.one a {
    text-decoration: none;
}

【问题讨论】:

  • 实际点击功能需要 javascript/jquery 才能获得永久效果。

标签: html css button onclick


【解决方案1】:

您应该应用以下样式:

#button:active {
    vertical-align: top;
    padding: 8px 13px 6px;
}

这会给你必要的效果,演示here。

【讨论】:

    【解决方案2】:

    按下整个按钮。我建议这个按钮看起来不错。

    #button:active {
        position: relative;
        top: 1px;
    }
    

    如果您只想推送文本,请增加顶部填充并减少底部填充。 您也可以使用行高。

    【讨论】:

      【解决方案3】:

      这是我制作的按下按钮示例:

      <div>
          <form id="forminput" action="action" method="POST">
             ...
          </form>
          <div style="right: 0px;bottom: 0px;position: fixed;" class="thumbnail">
              <div class="image">
                  <a onclick="document.getElementById('forminput').submit();">
                      <img src="images/button.png" alt="Some awesome text">
                  </a>
              </div>
          </div>
      </div>
      

      CSS 文件:

      .thumbnail {
          width: 128px;
          height: 128px;
      }
      
      .image {
          width: 100%;
          height: 100%;    
      }
      
      .image img {
          -webkit-transition: all .25s ease; /* Safari and Chrome */
          -moz-transition: all .25s ease; /* Firefox */
          -ms-transition: all .25s ease; /* IE 9 */
          -o-transition: all .25s ease; /* Opera */
          transition: all .25s ease;
          max-width: 100%;
          max-height: 100%;
      }
      
      .image:hover img {
          -webkit-transform:scale(1.05); /* Safari and Chrome */
          -moz-transform:scale(1.05); /* Firefox */
          -ms-transform:scale(1.05); /* IE 9 */
          -o-transform:scale(1.05); /* Opera */
           transform:scale(1.05);
      }
      
      .image:active img {
          -webkit-transform:scale(.95); /* Safari and Chrome */
          -moz-transform:scale(.95); /* Firefox */
          -ms-transform:scale(.95); /* IE 9 */
          -o-transform:scale(.95); /* Opera */
           transform:scale(.95);
      }
      

      尽情享受吧!

      【讨论】:

      • 这太棒了,除了我的特定设置中的任何原因,在 Chrome 中,它会在过渡期间突然对 (8pt) 文本进行抗锯齿处理,并在完成后删除抗锯齿,这看起来有点奇怪。否则真的很有创意地使用 CSS!
      【解决方案4】:

      JS 提供了正确执行此操作的工具。试试演示 sn-p。

      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;
      }            
      
      
          
      <!DOCTYPE html>
      <html lang="en">
        <head>
          <meta charset=utf-8 />
          <meta name="description" content="3d Button Configuration" />
        </head>
      
        <body>
          <section id="box">
            <button>
              Submit
            </button>
          </section>
        </body>
      </html>

      【讨论】:

        猜你喜欢
        • 2023-02-13
        • 2014-07-05
        • 2015-12-04
        • 2023-04-08
        • 2016-02-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多