• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    利用div+css3实现一个背景渐变的button按钮的示例代码

    发布者: 晁5074 | 发布时间: 2025-8-16 19:14| 查看数: 14| 评论数: 0|帖子模式

    随着目前前端页面的需求不断提升,有些场景需要渐变背景的元素。本文利用div和css3新属性,实现了一个背景渐变的按钮,具体如下:
    1.background: linear-gradient 背景为渐变色属性
    2.利用css3中动画特性animation,实现背景从左至右变化(color_move)
    3.为了实现渐变效果,将background的宽度拉长至400%
    上代码:
    html:
    1. <div class="btn_demo">
    2.         <div class="text">体 验</div>
    3.         <div class="arrow">»</div>
    4. </div>
    复制代码
    css:
    1. @keyframes arrow_move {
    2.    /* 开始状态 */
    3.     0% {
    4.         left: 130px;
    5.     }
    6.         /* 结束状态 */
    7.     100% {
    8.         left: 140px;
    9.     }
    10. }
    11. @keyframes color_move {
    12.     /* 开始状态 */
    13.     0% {
    14.         background-position: 0% 0%; /* 水平位置 垂直位置 */
    15.     }
    16.     50% {
    17.         background-position: 50% 0%;
    18.     }
    19.         /* 结束状态 */
    20.     100% {
    21.         background-position: 100% 0%;
    22.     }
    23. }
    24. .btn_demo {
    25.     width:180px;
    26.     height:60px;
    27.     border-radius: 10px;
    28.     position: relative;
    29.     background: linear-gradient( 90deg, #373d42 0%, #2679dd 50%, #373d42 100%);
    30.     background-size: 400% 100%;
    31.     animation: color_move 5s infinite ease-in-out alternate;
    32.     cursor: pointer;
    33. }
    34. .btn_demo:hover {
    35.     background: #2679dd;
    36. }
    37. .btn_demo:active {
    38.     background: #373d42;
    39. }
    40. .btn_demo > .text {
    41.     /* background: yellow; */
    42.     width: 50px;
    43.     text-align: center;
    44.     position: absolute;
    45.     left: 50%;
    46.     top: 50%;
    47.     transform: translate(-50%,-50%);
    48.     font-size: 20px;
    49.     color: #fff;
    50.     font-weight: bold;
    51. }
    52. .btn_demo > .arrow {
    53.     /* background: green; */
    54.     width: 20px;
    55.     text-align: center;
    56.     position: absolute;
    57.     font-size: 30px;
    58.     color: #fff;
    59.     top: 46%;
    60.     transform: translateY(-50%);
    61.     left: 130px; /* 移动130~150px */
    62.     /* 调用动画 */
    63.     animation-name: arrow_move;
    64.     /* 持续时间 */
    65.     animation-duration: 1s;
    66.     /* 无限播放 */
    67.     animation-iteration-count: infinite;
    68. }
    复制代码
    效果如下:


    到此这篇关于利用div+css3实现一个背景渐变的button按钮的示例代码的文章就介绍到这了,更多相关div+css3背景渐变按钮内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!

    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表