jquery - 如何像 8 球一样旋转 div
问题描述
我想旋转一个像 8 球一样的圆形 div。就像 8 球一样,这个 div 里面有一个数字,背景为白色。球如何旋转,就像这个 div 应该通过单击按钮旋转。此外,旋转时数字应该改变。
我已经完成了我的想法但失败了,它仅在重新加载并且旋转不正确并且我也不知道如何更改数字时才有效。
* {
box-sizing: border-box;
}
.eight-ball {
width: 400px;
height: 400px;
overflow: hidden;
position: relative;
border-radius: 50%;
display: inline-block;
background: rgb(33, 139, 220);
background: -moz-linear-gradient(top, rgba(33, 139, 220, 1) 0%, rgba(14, 92, 154, 1) 100%);
background: -webkit-linear-gradient(top, rgba(33, 139, 220, 1) 0%, rgba(14, 92, 154, 1) 100%);
background: linear-gradient(to bottom, rgba(33, 139, 220, 1) 0%, rgba(14, 92, 154, 1) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#218bdc', endColorstr='#0e5c9a', GradientType=0);
}
.eight-ball-inner-white {
width: 300px;
height: 300px;
overflow: hidden;
position: absolute;
top: 30px;
left: 6px;
border-radius: 50%;
display: inline-block;
transition: all ease 0.5s;
background: rgb(224, 235, 245);
background: -moz-linear-gradient(top, rgba(224, 235, 245, 1) 0%, rgba(190, 205, 214, 1) 100%);
background: -webkit-linear-gradient(top, rgba(224, 235, 245, 1) 0%, rgba(190, 205, 214, 1) 100%);
background: linear-gradient(to bottom, rgba(224, 235, 245, 1) 0%, rgba(190, 205, 214, 1) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#e0ebf5', endColorstr='#becdd6', GradientType=0);
}
.eight-ball-inner-white span {
color: #222b32;
position: absolute;
top: 50%;
left: 50%;
font-size: 152px;
transform: translate(-50%, -50%) rotate(16deg);
}
.rotate-ball {
-webkit-animation: rotate-ball linear 2s;
-moz-animation: rotate-ball linear 2s;
-ms-animation: rotate-ball linear 2s;
-o-animation: rotate-ball linear 2s;
animation: rotate-ball linear 2s;
}
.rotate-ball .spin-ball {
-webkit-animation: ballspin linear 2s;
-moz-animation: ballspin linear 2s;
-ms-animation: ballspin linear 2s;
-o-animation: ballspin linear 2s;
animation: ballspin linear 2s;
}
@-webkit-keyframes ballspin {
0% {
left: 6px;
top: 30px;
}
50% {
left: 170%;
top: 150px;
}
100% {
left: 6%;
top: 30px;
}
}
@-webkit-keyframes rotate-ball {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(120deg);
}
100% {
transform: rotate(220deg);
}
}
<div class="eight-ball rotate-ball">
<div class="eight-ball-inner-white spin-ball">
<span>88</span>
</div>
</div>
<button>Generate Ball</button>
解决方案
$(function(){
$('#genBall').on("click", function () {
$('.eight-ball').addClass("rotate-ball");
setTimeout(RemoveClass, 1200);
});
function RemoveClass() {
$('.eight-ball').removeClass("rotate-ball");
}
});
.eight-ball {
width: 500px;
height: 500px;
margin: auto;
overflow: hidden;
position: relative;
border-radius: 50%;
background: #1f84d0;
display: inline-table;
transition: all ease 0.5s;
transform-style: preserve-3d;
}
.eight-ball:after {
content: "";
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
opacity: 0.7;
border-radius: 50%;
transform: rotate(-137deg);
box-shadow: inset 50px 0 70px 6px rgb(0, 0, 0),
inset -3px 0 6px 2px rgba(255, 255, 255, 0.8);
}
.eight-ball-inner-white {
width: 400px;
height: 400px;
overflow: hidden;
position: absolute;
border-radius: 50%;
display: inline-block;
transition: all ease 0.5s;
background: rgb(224,235,245);
transform-style: preserve-3d;
transform: translate(6px,30px);
/*box-shadow: 0px 0px 27px rgba(0,0,0,0.5);*/
-webkit-animation: firstrotate ease-in 3s;
-moz-animation: firstrotate ease-in 3s;
-ms-animation: firstrotate ease-in 3s;
-o-animation: firstrotate ease-in 3s;
animation: firstrotate ease-in 3s;
background: -moz-linear-gradient(top, rgba(224,235,245,1) 0%, rgba(190,205,214,1) 100%);
background: -webkit-linear-gradient(top, rgba(224,235,245,1) 0%,rgba(190,205,214,1) 100%);
background: linear-gradient(to bottom, rgba(224,235,245,1) 0%,rgba(190,205,214,1) 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#e0ebf5', endColorstr='#becdd6',GradientType=0 );
}
.rotate-ball .spin-ball {
-webkit-animation: ballanim linear 4s;
-moz-animation: ballanim linear 4s;
-ms-animation: ballanim linear 4s;
-o-animation: ballanim linear 4s;
animation: ballanim linear 4s;
}
@keyframes ballanim {
0% {transform: translate(6px,30px) scale(1) rotateY(0deg) rotateX(0deg); width: 400px;}
50% {transform: translate(250%,30px) scale(0.3) rotateY(-60deg) rotateX(-5deg) ; width: 200px;}
100% {transform: translate(300%,30px) scale(0.2) rotateY(-60deg) rotateX(-5deg) ; width: 200px}
}
@-webkit-keyframes ballanim {
0% {transform: translate(6px,30px) scale(1) rotateY(0deg) rotateX(0deg) skew(0deg, 0deg); width: 400px;}
50% {transform: translate(250%,30px) scale(0.3) rotateY(-60deg) rotateX(-5deg) skew(-10deg, -10deg); width: 200px;}
100% {transform: translate(300%,30px) scale(0.2) rotateY(-60deg) rotateX(-5deg) skew(-10deg, -10deg); width: 200px;}
}
@keyframes firstrotate {
0% {transform: translate(-100%,30px) scale(0.3) rotateY(-80deg); width: 250px;}
50% {transform: translate(6px,30px) scale(1) rotateY(0deg); width: 400px;}
100% {transform: translate(6px,30px) scale(1) rotateY(0deg); width: 400px;}
}
@-webkit-keyframes firstrotate {
0% {transform: translate(-100%,30px) scale(0.3) rotateY(-80deg); width: 250px;}
50% {transform: translate(6px,30px) scale(1) rotateY(0deg); width: 400px;}
100% {transform: translate(6px,30px) scale(1) rotateY(0deg); width: 400px;}
}
.eight-ball-inner-white span {
color: #222b32;
position: absolute;
top: 50%;
left: 50%;
line-height: 0;
font-size: 152px;
font-weight: bold;
transform: translate(-50%,-50%) rotate(10deg);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="eight-ball">
<div class="eight-ball-inner-white spin-ball">
<span>GO</span>
</div>
</div>
<button id="genBall">Rotate</button>
推荐阅读
- python - Python:在绘图中编辑大小字体的问题
- python - 如何将列表与用户输入匹配
- javascript - 关闭标签页后如何在IOS中保存PWA状态?
- laravel - Laravel Nova 重置密码问题
- c - 结构元素的初始化
- javascript - 如何在自动增长的textarea中设置scrollHeight?
- html - CSS 媒体查询在 5 个特定的分辨率大小处中断....什么?
- ios - 如何使用 RxSwift 从网络请求中获取错误
- c# - WPF OnPreviewKeyDown 在只读时不能与 Textblock 或 Textbox 一起使用
- amazon-web-services - 创建 AWS 部署脚本时如何在 .gitignore 中声明 application.yml?