CSS特效---纯CSS实现点击切换按钮

1、演示

2、一切尽在代码中

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }

      body {
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: #0e5271;
      }

      span {
        display: inline-block;
        width: 600px;
        height: 300px;
        background-color: #ccc;
        border-radius: 160px;
        margin: 100px auto;
        padding: 10px;
        transition: 0.3s all ease-in-out;
      }

      span::after {
        position: absolute;
        content: "";
        width: 300px;
        height: 300px;
        border-radius: 50%;
        background-color: #fff;
        transition: 0.3s all ease-in-out;
      }

      input[type="checkbox"]:checked + span {
        background-color: greenyellow;
      }

      input[type="checkbox"]:checked + span::after {
        transform: translateX(300px);
      }

      input[type="checkbox"] {
        display: none;
      }

      input[type="checkbox"]:active + span::after {
        width: 350px;
      }

      input[type="checkbox"]:checked:active + span::after {
        transform: translateX(250px);
      }
    </style>
  </head>

  <body>
    <label for="toggle">
      <input type="checkbox" id="toggle" />
      <span></span>
    </label>
  </body>
</html>

 

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
制作播放暂停按钮可以使用HTML5的`<video>`标签和JavaScript来实现。下面是一个示例代码: ```html <!DOCTYPE html> <html> <head> <title>视频播放器</title> <style> /* 播放按钮和暂停按钮样式 */ .play-button, .pause-button { width: 50px; height: 50px; border-radius: 50%; background-color: #ccc; display: flex; justify-content: center; align-items: center; cursor: pointer; margin: 10px; } /* 暂停按钮样式 */ .pause-button { display: none; } </style> </head> <body> <video src="video.mp4" controls></video> <div class="play-button"></div> <div class="pause-button"></div> <script> // 获取视频元素 var video = document.querySelector('video'); // 获取播放按钮和暂停按钮 var playButton = document.querySelector('.play-button'); var pauseButton = document.querySelector('.pause-button'); // 点击播放按钮 playButton.onclick = function() { // 隐藏播放按钮,显示暂停按钮 playButton.style.display = 'none'; pauseButton.style.display = 'block'; // 播放视频 video.play(); }; // 点击暂停按钮 pauseButton.onclick = function() { // 隐藏暂停按钮,显示播放按钮 pauseButton.style.display = 'none'; playButton.style.display = 'block'; // 暂停视频 video.pause(); }; </script> </body> </html> ``` 关于CSS播放暂停按钮切换动画特效,可以使用CSS3的`transition`属性来实现。下面是一个示例代码: ```css /* 播放按钮和暂停按钮样式 */ .play-button, .pause-button { width: 50px; height: 50px; border-radius: 50%; background-color: #ccc; display: flex; justify-content: center; align-items: center; cursor: pointer; margin: 10px; transition: transform .2s ease-in-out; } /* 暂停按钮样式 */ .pause-button { display: none; } /* 鼠标悬停时的样式 */ .play-button:hover, .pause-button:hover { transform: scale(1.1); } /* 切换动画特效 */ .play-button.hide, .pause-button.show { transform: scale(0); } .pause-button.show, .play-button.hide { transform: scale(1); } ``` 在JavaScript代码中,当点击播放按钮时,使用`element.classList`属性来给播放按钮添加`hide`类,给暂停按钮添加`show`类;当点击暂停按钮时,使用`element.classList`属性来给暂停按钮添加`hide`类,给播放按钮添加`show`类。这样就可以通过CSS3的`transition`属性来实现按钮切换的动画特效了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bbamx.

谢谢您

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值