- 原本是一个评分页面,点击可以让评分星星移动.动画做的很好看,就想把它弄到本地项目当作加载页面,让星星自己动起来.
- 本来以为稍微改一下逻辑就行了的,结果看到那个react的代码是一点也不会😵💫然后就搞歪门邪道,比如
ReactDOM.render(React.createElement(Rating, { initialIndex: 0 }), document.getElementById('root'));
可以简单的控制元素的创建,那么我弄个setInterval,然后不断setTimeout回调并且清除root的innerhtml再重新创建,结果是不行的,dom没了但是react的事件没有卸载.最后找chat帮忙弄好了
(星星会自己动)
- index.html
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0">
<title>测试</title>
<link rel="stylesheet" href="./style.css">
<script src="./rely1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
function fixHeight() {
var headerHeight = $("#switcher").height();
$("#iframe").attr("height", $(window).height()-54+ "px");
}
$(window).resize(function () {
fixHeight();
}).resize();
$('.icon-monitor').addClass('active');
$(".icon-mobile-3").click(function () {
$("#by").css("overflow-y", "auto");
$('#iframe-wrap').removeClass().addClass('mobile-width-3');
$('.icon-tablet,.icon-mobile-1,.icon-monitor,.icon-mobile-2,.icon-mobile-3').removeClass('active');
$(this).addClass('active');
return false;
});
$(".icon-mobile-2").click(function () {
$("#by").css("overflow-y", "auto");
$('#iframe-wrap').removeClass().addClass('mobile-width-2');
$('.icon-tablet,.icon-mobile-1,.icon-monitor,.icon-mobile-2,.icon-mobile-3').removeClass('active');
$(this).addClass('active');
return false;
});
$(".icon-mobile-1").click(function () {
$("#by").css("overflow-y", "auto");
$('#iframe-wrap').removeClass().addClass('mobile-width');
$('.icon-tablet,.icon-mobile,.icon-monitor,.icon-mobile-2,.icon-mobile-3').removeClass('active');
$(this).addClass('active');
return false;
});
$(".icon-tablet").click(function () {
$("#by").css("overflow-y", "auto");
$('#iframe-wrap').removeClass().addClass('tablet-width');
$('.icon-tablet,.icon-mobile-1,.icon-monitor,.icon-mobile-2,.icon-mobile-3').removeClass('active');
$(this).addClass('active');
return false;
});
$(".icon-monitor").click(function () {
$("#by").css("overflow-y", "hidden");
$('#iframe-wrap').removeClass().addClass('full-width');
$('.icon-tablet,.icon-mobile-1,.icon-monitor,.icon-mobile-2,.icon-mobile-3').removeClass('active');
$(this).addClass('active');
return false;
});
});
</script>
<script type="text/javascript">
function Responsive($a) {
if ($a == true) $("#Device").css("opacity", "100");
if ($a == false) $("#Device").css("opacity", "0");
$('#iframe-wrap').removeClass().addClass('full-width');
$('.icon-tablet,.icon-mobile-1,.icon-monitor,.icon-mobile-2,.icon-mobile-3').removeClass('active');
$(this).addClass('active');
return false;
};
</script>
</head>
<body id="by">
<h2 style="display: flex; justify-content: center; align-items: center; position: absolute; top: 40%; left: 50%; transform: translate(-50%, -50%); color: rgb(255, 255, 255);">页面发生错误,请检查网络后重新尝试或联系管理员处理</h2>
<div id="root"></div>
<div style="display: grid;place-items: center;">
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;position: absolute;top: 100px;left: 200px;">
<symbol xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 34" id="star">
<path fill="currentColor" d="M19.6859343,0.861782958 L24.8136328,8.05088572 C25.0669318,8.40601432 25.4299179,8.6717536 25.8489524,8.80883508 L34.592052,11.6690221 C35.6704701,12.021812 36.2532905,13.1657829 35.8938178,14.2241526 C35.8056709,14.4836775 35.6647294,14.7229267 35.4795411,14.9273903 L29.901129,21.0864353 C29.5299163,21.4962859 29.3444371,22.0366367 29.3872912,22.5833831 L30.1116131,31.8245163 C30.1987981,32.9368499 29.3506698,33.9079379 28.2172657,33.993502 C27.9437428,34.0141511 27.6687736,33.9809301 27.4085205,33.8957918 L18.6506147,31.0307612 C18.2281197,30.8925477 17.7713439,30.8925477 17.3488489,31.0307612 L8.59094317,33.8957918 C7.51252508,34.2485817 6.34688429,33.6765963 5.98741159,32.6182265 C5.90066055,32.3628116 5.86681029,32.0929542 5.88785051,31.8245163 L6.61217242,22.5833831 C6.65502653,22.0366367 6.46954737,21.4962859 6.09833466,21.0864353 L0.519922484,14.9273903 C-0.235294755,14.0935658 -0.158766688,12.8167745 0.690852706,12.0755971 C0.899189467,11.8938516 1.14297067,11.7555303 1.40741159,11.6690221 L10.1505113,8.80883508 C10.5695458,8.6717536 10.9325319,8.40601432 11.1858308,8.05088572 L16.3135293,0.861782958 C16.9654141,-0.0521682813 18.2488096,-0.274439442 19.1800736,0.365326425 C19.3769294,0.500563797 19.5481352,0.668586713 19.6859343,0.861782958 Z"></path>
<path class="shadow" d="M18.7022469,29.7633426 L29.1611722,33.6861584 C28.8859085,33.8576358 28.5650147,33.9672494 28.2172657,33.993502 C27.9437428,34.0141511 27.6687736,33.9809301 27.4085205,33.8957918 L18.6506147,31.0307612 C18.2281197,30.8925477 17.7713439,30.8925477 17.3488489,31.0307612 L8.59094317,33.8957918 C7.98083887,34.0953792 7.34281791,33.9989813 6.83864817,33.6859784 L17.2977531,29.7633426 C17.7505234,29.5935537 18.2494766,29.5935537 18.7022469,29.7633426 Z"></path>
</symbol>
</svg>
</div>
<script type="text/javascript" src="/static/js/jquery.min.js"></script>
<script type="text/javascript" src="/static/js/jquery.qrcode.min.js"></script>
<script src="./script.js"></script>
<script>ReactDOM.render(React.createElement(Rating, { initialIndex: 2 }), document.getElementById('root'));</script>
</body>
</html>
- script.js
"use strict";
const { FC, useCallback, useState, useEffect, useRef } = React;
const Rating = ({ initialIndex, onChange }) => {
const [current, setCurrent] = useState(initialIndex);
const [last, setLast] = useState(null);
const [animationClass, setAnimationClass] = useState(undefined);
const ratingRef = useRef(null);
const liRefs = useRef([]);
useEffect(() => {
if (animationClass) {
setTimeout(() => {
var _a, _b;
setAnimationClass('');
(_a = liRefs.current[last !== null && last !== undefined ? last : 0])?.classList.remove('move-from');
(_b = liRefs.current[current !== null && current !== undefined ? current : 0])?.classList.remove('move-to');
}, 800);
}
}, [animationClass, last]);
useEffect(() => {
// 自动切换
const intervalId = setInterval(() => {
const newIndex = (current + 1) % 5; // 这里假设总共有5个评分项
handleRatingChange(newIndex);
}, 900); // 设置切换时间间隔,单位是毫秒
return () => clearInterval(intervalId); // 在组件卸载时清除 interval
}, [current]);
const handleRatingChange = useCallback((index) => {
var _a, _b;
if (!ratingRef.current || current === index) {
return;
}
const li = liRefs.current[index];
const x = (_a = li?.offsetLeft) !== null && _a !== undefined ? _a : 0;
setLast(current);
setCurrent(-1);
setTimeout(() => setCurrent(index), 800);
if (onChange) {
onChange(index);
}
li?.classList.add('move-to');
(_b = liRefs.current[last !== null && last !== undefined ? last : 0])?.classList.add('move-from');
ratingRef.current.style.setProperty('--x', `${x}px`);
setAnimationClass(index > (last !== null && last !== undefined ? last : 0) ? 'animate-right' : 'animate-left');
}, [ratingRef, liRefs, current, last, onChange]);
return (React.createElement("nav", { ref: ratingRef, className: `rating ${animationClass}` },
React.createElement("ul", null, [1, 2, 3, 4, 5].map((_, i) => (React.createElement("li", { key: i, ref: (el) => liRefs.current[i] = el, className: `${current === i ? 'current' : ''} ${i < current ? 'active' : ''}`, onClick: () => handleClick(i) },
i >= 4 && React.createElement("span", null),
React.createElement("svg", null,
React.createElement("use", { xlinkHref: "#star" })))))),
React.createElement("div", { className: `${animationClass ? 'show' : 'hide'} moving` },
React.createElement("span", null,
React.createElement("svg", null,
React.createElement("use", { xlinkHref: "#star" }))))));
};
ReactDOM.render(React.createElement(Rating, { initialIndex: 0 }), document.getElementById('root'));
- style.css
.rating {
--active: #FFED76;
--active-pale: rgba(255, 237, 118, .36);
--inactive: #121022;
--face-active: #121621;
--face-inactive: #353156;
display: flex;
position: absolute;
top: 60%;
left: 47%;
transform: translate(-50%, -50%);
}
.rating ul {
margin: 0;
padding: 0;
list-style: none;
display: flex;
color: var(--inactive);
}
.rating ul li {
--face: var(--face-inactive);
cursor: pointer;
position: relative;
}
.rating ul li:before, .rating ul li:after {
content: "";
position: absolute;
z-index: 2;
transition: all 0.2s ease;
}
.rating ul li:before {
--r: 0deg;
width: 2px;
height: 2px;
border-radius: 1px;
top: 15px;
left: 13px;
transform: rotate(var(--r));
filter: drop-shadow(8px 0 0 var(--face));
}
.rating ul li span {
width: 2px;
height: 2px;
display: block;
position: absolute;
left: 50%;
top: 50%;
border-radius: 50%;
margin: -1px 0 0 -1px;
transform: scale(0.6);
opacity: 0;
box-shadow: 16px -16px 0 var(--active-pale), -16px -16px 0 var(--active-pale), -21px 8px 0 var(--active-pale), 21px 8px 0 var(--active-pale), 0 22px 0 var(--active-pale);
}
.rating ul li:nth-child(1):after {
width: 10px;
height: 10px;
top: 20px;
left: 13px;
border-radius: 50%;
border: 1px solid transparent;
border-top-color: var(--face);
}
.rating ul li:nth-child(3):after {
width: 6px;
left: 15px;
top: 20px;
height: 1px;
background: var(--face);
}
.rating ul li:nth-child(4):after {
width: 10px;
height: 10px;
top: 12px;
left: 13px;
border-radius: 50%;
border: 1px solid transparent;
border-bottom-color: var(--face);
}
.rating ul li:nth-child(4).current {
-webkit-animation: active-4 0.4s ease;
animation: active-4 0.4s ease;
}
.rating ul li:nth-child(4).current span {
-webkit-animation: active-span 0.32s ease;
animation: active-span 0.32s ease;
}
.rating ul li:nth-child(5):after {
width: 6px;
height: 3px;
left: 15px;
top: 20px;
border-radius: 0 0 3px 3px;
background: var(--face);
}
.rating ul li:nth-child(5).current {
-webkit-animation: active-5 0.72s ease;
animation: active-5 0.72s ease;
}
.rating ul li:nth-child(5).current span {
-webkit-animation: active-span 0.32s ease;
animation: active-span 0.32s ease;
}
.rating ul li.current {
--face: var(--face-active);
}
.rating ul li.current svg {
color: var(--active);
fill: rgba(0, 0, 0, 0.2);
}
.rating ul li:not(.current) svg {
transition: transform 0.2s ease;
}
.rating ul li:not(.current):active svg {
transform: scale(0.9);
}
.rating ul li:not(:last-child) {
margin: 0 16px 0 0;
}
.rating ul li:not(:last-child):before {
background: var(--face);
}
.rating ul li:not(:last-child).current:before {
-webkit-animation: blink 3s linear infinite;
animation: blink 3s linear infinite;
}
.rating ul li:last-child:before {
--r: -45deg;
width: 3px;
height: 3px;
border-top: 1px solid var(--face);
border-right: 1px solid var(--face);
border-radius: 0 1px 0 0;
left: 12px;
filter: drop-shadow(6px 6px 0 var(--face));
}
.rating svg {
width: 36px;
height: 34px;
display: block;
fill: rgba(0, 0, 0, 0.04);
}
.rating .moving {
left: 0;
top: 0;
position: absolute;
z-index: 1;
transform: translateX(var(--x));
transition: transform 0.3s cubic-bezier(0, 0, 0.265, 1.1) 0.24s;
}
.rating .moving.hide {
opacity: 0;
}
.rating .moving span {
display: block;
}
.rating .moving span svg {
color: var(--active);
fill: rgba(0, 0, 0, 0.2);
}
.rating.animate-left .moving span, .rating.animate-right .moving span {
-webkit-animation: scale 0.28s linear 0.24s;
animation: scale 0.28s linear 0.24s;
}
.rating.animate-left li.move-to:before {
-webkit-animation: move-to-left 0.31s ease 0.36s;
animation: move-to-left 0.31s ease 0.36s;
}
.rating.animate-left li.move-from:before {
-webkit-animation: move-to-right 0.28s ease;
animation: move-to-right 0.28s ease;
}
.rating.animate-left .moving {
-webkit-animation: double-left 0.32s linear 0.24s;
animation: double-left 0.32s linear 0.24s;
}
.rating.animate-left .moving span svg {
-webkit-animation: left 0.3s ease, right-end 0.4s ease 0.4s;
animation: left 0.3s ease, right-end 0.4s ease 0.4s;
}
.rating.animate-right li.move-to:before {
-webkit-animation: move-to-right 0.31s ease 0.36s;
animation: move-to-right 0.31s ease 0.36s;
}
.rating.animate-right li.move-from:before {
-webkit-animation: move-to-left 0.28s ease;
animation: move-to-left 0.28s ease;
}
.rating.animate-right .moving {
-webkit-animation: double-right 0.32s linear 0.24s;
animation: double-right 0.32s linear 0.24s;
}
.rating.animate-right .moving span svg {
-webkit-animation: right 0.3s ease, left-end 0.4s ease 0.4s;
animation: right 0.3s ease, left-end 0.4s ease 0.4s;
}
@-webkit-keyframes active-4 {
40% {
transform: scale(1.25);
}
}
@keyframes active-4 {
40% {
transform: scale(1.25);
}
}
@-webkit-keyframes active-span {
60% {
opacity: 1;
}
100% {
transform: scale(1.16);
opacity: 0;
}
}
@keyframes active-span {
60% {
opacity: 1;
}
100% {
transform: scale(1.16);
opacity: 0;
}
}
@-webkit-keyframes active-5 {
15% {
transform: rotate(180deg) scale(1.1);
}
30% {
transform: rotate(360deg) scale(1.2);
}
70% {
transform: rotate(360deg) translateY(14%) scaleY(0.72);
}
100% {
transform: rotate(360deg);
}
}
@keyframes active-5 {
15% {
transform: rotate(180deg) scale(1.1);
}
30% {
transform: rotate(360deg) scale(1.2);
}
70% {
transform: rotate(360deg) translateY(14%) scaleY(0.72);
}
100% {
transform: rotate(360deg);
}
}
@-webkit-keyframes double-right {
60% {
filter: drop-shadow(-5px 0 1px var(--active-pale)) drop-shadow(4px 0 1px var(--active-pale));
}
}
@keyframes double-right {
60% {
filter: drop-shadow(-5px 0 1px var(--active-pale)) drop-shadow(4px 0 1px var(--active-pale));
}
}
@-webkit-keyframes scale {
60% {
transform: scaleX(1.32);
filter: blur(0.5px);
}
}
@keyframes scale {
60% {
transform: scaleX(1.32);
filter: blur(0.5px);
}
}
@-webkit-keyframes blink {
0%, 5%, 15%, 100% {
transform: scaleY(1);
}
10% {
transform: scaleY(0.4);
}
}
@keyframes blink {
0%, 5%, 15%, 100% {
transform: scaleY(1);
}
10% {
transform: scaleY(0.4);
}
}
@-webkit-keyframes double-left {
60% {
filter: drop-shadow(5px 0 1px var(--active-pale)) drop-shadow(-4px 0 1px var(--active-pale));
}
}
@keyframes double-left {
60% {
filter: drop-shadow(5px 0 1px var(--active-pale)) drop-shadow(-4px 0 1px var(--active-pale));
}
}
@-webkit-keyframes right {
0%, 100% {
transform-origin: 17% 100%;
}
50% {
transform: rotate(-12deg) skewX(12deg);
}
}
@keyframes right {
0%, 100% {
transform-origin: 17% 100%;
}
50% {
transform: rotate(-12deg) skewX(12deg);
}
}
@-webkit-keyframes left {
0%, 100% {
transform-origin: 83% 100%;
}
50% {
transform: rotate(12deg) skewX(-12deg);
}
}
@keyframes left {
0%, 100% {
transform-origin: 83% 100%;
}
50% {
transform: rotate(12deg) skewX(-12deg);
}
}
@-webkit-keyframes right-end {
0%, 100% {
transform-origin: 17% 100%;
}
40% {
transform: rotate(-6deg) skewX(4deg) scaleX(0.92);
}
70% {
transform: rotate(1deg) skewX(-8deg) scaleX(1.04);
}
}
@keyframes right-end {
0%, 100% {
transform-origin: 17% 100%;
}
40% {
transform: rotate(-6deg) skewX(4deg) scaleX(0.92);
}
70% {
transform: rotate(1deg) skewX(-8deg) scaleX(1.04);
}
}
@-webkit-keyframes left-end {
0%, 100% {
transform-origin: 83% 100%;
}
40% {
transform: rotate(6deg) skewX(-4deg) scaleX(0.92);
}
70% {
transform: rotate(-1deg) skewX(8deg) scaleX(1.04);
}
}
@keyframes left-end {
0%, 100% {
transform-origin: 83% 100%;
}
40% {
transform: rotate(6deg) skewX(-4deg) scaleX(0.92);
}
70% {
transform: rotate(-1deg) skewX(8deg) scaleX(1.04);
}
}
@-webkit-keyframes move-to-right {
40% {
transform: translateX(3px) rotate(var(--r));
}
}
@keyframes move-to-right {
40% {
transform: translateX(3px) rotate(var(--r));
}
}
@-webkit-keyframes move-to-left {
40% {
transform: translateX(-3px) rotate(var(--r));
}
}
@keyframes move-to-left {
40% {
transform: translateX(-3px) rotate(var(--r));
}
}
html {
box-sizing: border-box;
-webkit-font-smoothing: antialiased;
}
* {
box-sizing: inherit;
}
*:before, *:after {
box-sizing: inherit;
}
body {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background-color: #020112;
overflow: hidden;
width: 800px;
}
body:before {
width: 800px;
content: "";
position: absolute;
inset: 40% -60% 0 -60%;
background-image: radial-gradient(ellipse at bottom, #1D0559 0%, #020112 50%);
opacity: 0.4;
}
body .twitter {
position: fixed;
display: block;
right: 12px;
bottom: 12px;
}
body .twitter svg {
width: 32px;
height: 32px;
fill: #fff;
}