javascript - 如何实现类似于 http://thisiskiosk.com/ 的可滚动圆形文本效果
问题描述
如果您在此处查看KIOSK WEBSITE他们在 javascript 中有“我们已打开”循环类型(我知道如何做到这一点)但我不知道如何在滚动时实现这一点。就像文本如何移动向上或向下滚动时。如何在 HTML/CSS/JS 中获得它?
查看我在这里工作的代码 https://codepen.io/noel_emmanuel/pen/WJxRZW
HTML:
<!--just a container used to position in the page-->
<div class="container">
<!--the holders/targets for the text, reuse as desired-->
<div class="circTxt" id="test"></div>
</div>
<!--I told you it was simple! :)-->
CSS:
body {
background: #111;
}
.container {
/*centers in the container*/
text-align: center;
}
div.circTxt {
/*allows for centering*/
display: inline-block;
/*adjust as needed*/
margin-bottom: 128px;
color: whitesmoke;
}
JS:
function circularText(txt, radius, classIndex) {
txt = txt.split(""),
classIndex = document.getElementsByClassName("circTxt")[classIndex];
var deg = 360 / txt.length,
origin = 0;
txt.forEach((ea) => {
ea = `<p style='height:${radius}px;position:absolute;transform:rotate(${origin}deg);transform-origin:0 100%'>${ea}</p>`;
classIndex.innerHTML += ea;
origin += deg;
});
}
circularText("WE ARE OPEN", 100, 0);
开放的建议。
解决方案
您可以在滚动事件上旋转它。这只是div
根据您滚动页面顶部的距离来旋转 。
我在文本中添加了一个height
和,并将其定位以查看效果。width
fixed
function circularText(txt, radius, classIndex) {
txt = txt.split(""),
classIndex = document.getElementsByClassName("circTxt")[classIndex];
var deg = 360 / txt.length,
origin = 0;
txt.forEach((ea) => {
ea = `<p style='height:${radius}px;position:absolute;transform:rotate(${origin}deg);transform-origin:0 100%'>${ea}</p>`;
classIndex.innerHTML += ea;
origin += deg;
});
}
circularText("WE ARE OPEN", 100, 0);
$(document).ready(function(){
$(window).scroll(function(e){
rotateText();
});
function rotateText(){
var scrolled = $(window).scrollTop();
$('div.circTxt').css('transform','rotate('+scrolled+'deg)');
}
});
body {
background: #111;
}
.container {
/*centers in the container*/
text-align: center;
height: 4000px;
}
div.circTxt {
/*allows for centering*/
display: inline-block;
/*adjust as needed*/
margin-bottom: 128px;
color: whitesmoke;
position: fixed;
height: 200px;
width: 200px;
transform-origin: 0% 59%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!--just a container used to position in the page-->
<div class="container">
<!--the holders/targets for the text, reuse as desired-->
<div class="circTxt" id="test"></div>
</div>
<!--I told you it was simple! :)-->
推荐阅读
- c# - 如何对表及其子节点的特定记录执行“复制”/“粘贴”?
- php - 获取选择输入 laravel 的“旧”值
- java - 如何在此代码中打印出方法“中心”的值 - Java
- linux - 防止在 ssh 命令中扩展/评估参数
- java - 使用专有 MySQL DB 对 Java 应用程序进行 Dockerizing
- python - Python 函数看不到导入的模块
- ios - swift firestore tinder - 获取用户并添加到数组以加载到屏幕上 - 高级
- javascript - Javascript“mousemove”事件有时不会触发
- javascript - hasOwnProperty 未捕获的类型错误
- c# - 如何使用Task异步加载Winforms中的数据