文章最后更新时间:2024年08月10日已超过844天没有更新。
这是一段展示环形文字的控制代码,可以根据input输入自己喜欢的内容,并可根据参数按钮进行文字角度的调整,简单易修改,将字符串分割成数组的形式,然后对数组中的每一项进行渲染并设置对应的旋转角度。
效果图片:

完整代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>利用JS+CSS实现环形文字的旋转展示</title>
<style>
.container {
position: relative;
}
.emblem span {
position: absolute;
display: inline-block;
left: 0;
right: 0;
top: 0;
bottom: 0;
font-size: 18px;
color: #37474f;
font-weight: 900;
transition: all 0.5s cubic-bezier(0, 0, 0, 1);
}
.emblem {
position: absolute;
left: 80px;
top: 80px;
width: 240px;
height: 240px;
border-radius: 50%;
color: #7D970B;
text-align: center;
overflow: hidden;
border: 5px solid red;
}
.rightConent {
/*width: 300px;*/
margin-left: 450px;
margin-top: 30px;
}
.btns {
display: flex;
margin: auto;
}
.btns > div {
border: 1px solid #7D970B;
border-radius: 10px;
padding: 20px;
margin: 10px;
}
.btns > div > div {
text-align: center;
border: coral 2px solid;
border-radius: 10px;
margin: 10px auto;
padding: 8px 20px;
cursor: pointer;
}
.btns > div > div:hover {
background-color: #FFC790
}
.inputBox {
display: flex;
align-items: center;
}
input {
outline: none;
display: block;
width: 300px;
height: 40px;
border: 2px solid orange;
border-radius: 10px;
color: #666;
text-indent: 20px;
}
.changeBtn {
width: 100px;
height: 40px;
border-radius: 8px;
background: #666666;
color: #ffffff;
text-align: center;
line-height: 40px;
margin-left: 10px;
}
</style>
</head>
<body>
<div>
<div id="emblem"></div>
</div>
<div>
<div>
<input type="text" value="" placeholder="请输入名称">
<div onclick="changeCompany()">提交变更</div>
</div>
<h2>调整角度</h2>
<div>
<div>
<div onclick="emblemDeg(-1)">-1</div>
<div onclick="emblemDeg(-5)">-1</div>
<div onclick="emblemDeg(-20)">-20</div>
</div>
<div>
<div onclick="emblemDeg(1)">+1</div>
<div onclick="emblemDeg(5)">+1</div>
<div onclick="emblemDeg(20)">+20</div>
</div>
</div>
</div>
<script src="https://www.cuinew.com/demo/view/js/2023110901/jquery-1.10.2.js"></script>
<script>
let startData = '我是环形文字'
let emblemText = startData.split('')
circleText('.emblem')
function changeCompany() {
let _p = document.getElementById('emblem')
_p.innerHTML = ''
let val = $('.company').val() ? $('.company').val() : '我是第二段环形文字'
emblemText = val.split('')
console.log(emblemText)
circleText('.emblem')
}
function circleText(el, str) {
let element = document.querySelector(el);
emblemText.forEach((v, i) => {
let span = document.createElement('span');
span.innerHTML = `<pre>${emblemText[i]}</pre>`
let deg = (i + 1) * 30 - 15;
span.style.transform = `rotateZ(${deg}deg)`
element.appendChild(span);
})
}
let arr = []
function emblemDeg(deg) {
arr.push(deg)
let emblem = document.getElementById('emblem');
emblem.style.transform = `rotateZ(${sum(arr)}deg)`
}
function sum(arr) {
let len = arr.length;
if (len == 0) {
return 0;
} else if (len == 1) {
return arr[0];
} else {
return arr[0] + sum(arr.slice(1));
}
}
</script>
</body>
</html>文章版权声明:除非注明,否则均为崔牛网原创文章,转载或复制请以超链接形式并注明出处。

