jquery - 如何像8球一样旋转一个div

标签 jquery html css css-transitions css-transforms

我想像 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>

关于jquery - 如何像8球一样旋转一个div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58316680/

相关文章:

javascript - #!链接中的主题标签和感叹号作为文件夹?

JavaScript/JQuery 从 Web API 检索 JSON 数据

c# - 无法通过警报调用 javascript 函数

html - position:relative,溢出:隐藏父级和绝对子级

html - 如何在表格单元格上添加边框,而不是在 html 上的整个表格?

javascript - 使用 jQuery 包含 JS 文件

javascript - jQuery 表单提交后返回空白页

javascript - 如果类存在则加载标记

html - 水平导航栏

html - 如何使 flexbox 元素适合它们的图像大小?