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

1782 0
Honkers 2022-10-19 16:06:32 | 显示全部楼层 |阅读模式
随着目前前端页面的需求不断提升,有些场景需要渐变背景的元素。本文利用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背景渐变按钮内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章,希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

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

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

特级红客

关注
  • 3159
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行