用CSS怎样做一个动画的订阅按钮效果?
Admin 2021-12-28 群英技术资讯 609 次浏览
订阅按钮在很多应用上都能看到,不同应用的订阅按钮有一定的区别,这篇主要给大家分享类似抖音的动画的订阅按钮效果,具体的效果如下,还是比较美观好看的,那么这样的效果要怎样做呢?
思路
toggleClass()
方法,添加删除类active
active
的元素设置样式,使用css的transition
属性,定义变化时间,速度等animation
为active
元素设置变化动画实现
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <link rel="stylesheet" href="style.css"> <title>Document</title> </head> <body> <!-- 简单起见,用了div。其实用button也行,需要设置一下样式才好看^_^--> <!-- 还没有学习<svg>,使用<svg>效果也许会更好--> <div id="followBtn"> <div id="line1"></div> <div id="line2"></div> </div> <script src="index.js"></script> </body> </html>
JS
$(function () { // jQuery入口函数 $('#followBtn').click(function (e) { // 绑定点击事件 $('#followBtn').toggleClass('active'); $('#line1').toggleClass('active'); $('#line2').toggleClass('active'); }); })
CSS
body { width: 1024px; margin: 0 auto; /* 居中 */ } #followBtn { position: relative; display: block; width: 100px; height: 100px; margin: 100px auto; border-radius: 100px; // 使div变为圆形 background-color: #ccc; transition: all linear .5s; // 定义样式转换时的过度动画的属性 } #followBtn.active { background-color: crimson; } #line1 { position: absolute; /*绝对定位,定位基于最近的一个已经定位(relative, absolute, fixed)的祖先元素*/ left: 25px; top: 46px; display: block; width: 50px; height: 8px; border-radius: 5px; background-color: crimson; transition: all linear .5s; } #line2 { position: absolute; left: 25px; top: 46px; display: block; width: 50px; height: 8px; border-radius: 8px; background-color: crimson; transform: rotate(90deg); /* 旋转90度 */ transition: all linear .5s; } #line1.active { background-color: #ccc; /*触发动画,forwards表示动画结束后,元素样式保留为动画的最后一个关键帧的样式*/ animation: line1 .5s ease-in-out forwards; } #line2.active { background-color: #ccc; animation: line2 .5s ease-in-out forwards; } /* @keyframes定义动画 */ @keyframes line1 { 50% { width: 8px; height: 8px; left: 20px; top: 52px; border-radius: 8px; } 100% { width: 30px; left: 20px; top: 52px; transform: rotate(45deg); } } @keyframes line2 { 50% { width: 8px; height: 8px; border-radius: 8px; left: 35px; } 100% { width: 50px; left: 35px; transform: rotate(-45deg); } }
分割线使用svg绘制对号最终效果
实现
HTML
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script> <link rel="stylesheet" href="style.css"> <title>Document</title> </head> <body> <button id="followBtn"> <div class="line"></div> <div class="line"></div> <!-- 使用svg绘制 --> <!-- stroke-linecap 设置折线两端为圆角 --> <!-- stroke-linejoin 设置折线拐角为圆角 --> <svg width="70px" height="70px" stroke-width="8" stroke-linecap="round" stroke-linejoin="round" fill="none"> <polyline points="10,37 30,57 60,17" style="stroke: crimson;"></polyline> </svg> </button> <script src="index.js"></script> </body> </html>
JS
一定要使用jQuery3,3以下的版本操作svg元素(添加类)时有bug,3修复了这个问题。
$(function () { $('#followBtn').click(function (e) { $('#followBtn').toggleClass('active'); $('.line').toggleClass('active'); $('polyline').toggleClass('active'); }); })
CSS
body { width: 1024px; margin: 0 auto; } #followBtn { position: relative; display: block; width: 100px; height: 100px; margin: 100px auto; border: none; border-radius: 100px; background-color: crimson; transition: all linear .5s; } #followBtn:focus { outline: none; /* 浏览器点击不会有蓝框 */ } #followBtn.active { background-color: #ccc; } .line { position: absolute; /*绝对定位,定位基于最近的一个已经定位(relative, absolute, fixed)的祖先元素*/ left: 25px; top: 46px; width: 50px; height: 8px; border-radius: 8px; background-color: #ccc; transition: ease-in 0; } .line:nth-child(1) { transform: rotate(90deg); } .line.active { animation: fade .5s forwards; } polyline { /* 属性stroke-dasharray设置线段缺口以形成曲线, * 当缺口足够大,看起来折线就隐藏了 * 属性stroke-dashoffset指定了dash模式到路径开始的距离,0时折线完全显示 */ stroke-dasharray: 80px; stroke-dashoffset: 80px; } polyline.active { animation: show .5s forwards; animation-delay: .5s; } @keyframes show { to { stroke-dashoffset: 0; } } @keyframes fade { to { opacity: 0; transform: rotate(360deg) scale(0.5, 0.5); } }
感悟
普通HTML元素和SVG元素的旋转方式不同:
普通HTML元素的transform-origin
默认为自身的中心SVG元素的transform-origin
默认为SVG画布的左上角
去除按钮点击后的蓝框,可以设置outline: none;
jQuery3以下的版本,不能正确的给修改SVG元素的类。
//.attr()方法对于SVG是有效的,所以如果你必须使用jQuery的话 // 使用 $("#item").attr("class", "oldclass newclass"); // 而不是 .addClass("newclass") // 使用 $("#item").attr("class", "oldclass"); // 而不是 .removeClass("newclass") // 原生JS解决办法 var element = document.getElementById("item"); // 使用 element.setAttribute("class", "oldclass newclass"); // 使用 element.setAttribute("class", "oldclass");
以上就是用CSS实现一个动画的订阅按钮效果的介绍,本文只是提供了一种实现思路,代码仅供参考,需要的朋友可以了解看看。想要了解更多订阅按钮效果的实现,大家可以继续浏览群英网络其他相关的文章。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
很多朋友对于CSS优先级不是很理解,其实我们能从值相加和bit位存储这两种方式来看,下文的介绍很详细,有需要的有需要的朋友可以了解看看,对大家理解CSS优先级会有一定的帮助。
今天给大家介绍下css3之border-radius属性的详细作用是什么,以及如何用css3中border-radius属性实现画圆,下文的讲解详细,步骤过程清晰,对大家进一步学习和理解相关知识有一定的帮助。有这方面学习需要的朋友就继续往下看吧!
XHTML是可扩展超文本标记语言,被广泛运用到程序当中,而学习XHTML语言也不是件容易的事情。下面的时间就跟随小编来了解下“框架标签使用”内容吧!
css计数器是一个通过变量来设置,根据规则递增变量。在web设计中,我们常会使用到有序列表,有序列表的好处就是能够清楚地展示网站内容,但是如果只是单纯的数字,那么页面不够美观,而且手动添加样式又很麻烦。如果我们使用CSS计数器就能很便捷的美化数字有序列表。下面我们一起来看看CSS计数器怎么用。
这篇文章主要介绍了css3翻牌翻数字的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008