html线条绕圆旋转,js围绕圆旋转

本文通过运用三角函数,解析如何在HTML、CSS和JavaScript中创建动态的圆形元素,实现鼠标点击时的平滑旋转效果。从代码实现到原理讲解,适合前端开发者深入理解动画原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实利用以前学过的三角函数的知识就可以解这道题

html

click me
click me
click me

css

html,body{

margin: 0;

padding: 0;

width: 100%;

height: 100%;

}

.box-wrap{

display: flex;

justify-content: space-around;

align-items: center;

width: 100%;

height: 100%;

}

.box{

width: 100px;

height: 100px;

background-color: #009a61;

border-radius: 50%;

text-align: center;

line-height: 100px;

color: #fff;

cursor: pointer;

}

.round{

border-radius: 50%;

position: absolute;

background-color: #009a61;

}

js

var app = {

init: function () {

this.move();

},

cEle: function (tagName, iClass) {

var tag = document.createElement(tagName);

tag.className = iClass ? iClass : '';

return tag;

},

css: function (ele, styles) {

for(var attr in styles){

ele['style'][attr] = styles[attr];

}

},

round: function (ele, x, y, r) {

var deg = 0;

var timer = null;

var _this = this;

var w = ele.offsetWidth;

var h = ele.offsetHeight;

var a,b;

clearInterval(timer);

timer = setInterval(function () {

deg += 2;

a = Math.sin(deg * Math.PI/180) * r;

b = Math.cos(deg * Math.PI/180) * r;

_this.css(ele, {

left: (x - w/2) + b + 'px',

top: (y - h/2) + a + 'px'

})

}, 30);

},

move: function () {

var aBox = document.querySelectorAll('.box');

var _this = this;

var body = document.body;

var left,top,r,ele,w;

[].slice.call(aBox).forEach(function (e) {

e.addEventListener('click', function () {

ele = _this.cEle('div', 'round');

w = Math.floor(Math.random() * 50 + 10);

left = this.offsetLeft + this.offsetWidth/2;

top = this.offsetTop + this.offsetHeight/2;

r = this.offsetWidth + Math.floor(Math.random() * 50);

_this.css(ele, {

width: w + 'px',

height: w + 'px'

})

body.appendChild(ele);

_this.round(ele, left, top, r);

}, false)

})

}

}

app.init();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值