弹窗
<!-- 弹窗 -->
<div id="modal">
<div class="mask flex justify-center items-center">
<div class="modal-layer">
<!-- 弹窗实例 -->
<!-- 弹窗 1 -->
<div id="modal_name1" class="modal"></div>
<!-- 弹窗 2 -->
<div id="modal_name2" class="modal"></div>
<!-- 关闭按钮 -->
<div class="btn btn-close"></div>
</div>
</div>
</div>
#modal{
display: none;
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
.mask{
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.6);
.modal-layer{
position: relative;
.modal{
display: none;
border-radius: 0.12rem;
background-color: #fff;
}
}
}
}
#modal_name1 { ... }
const $modal = $("#modal");
const $modal_children = $modal.find(".modal");
const $btn_close = $modal.find(".btn-close");
// 开启弹窗
function openPopup(sel){
$(sel).show();
$modal.show();
// $modal.fadeIn();
}
// 跳转到另一弹窗
function redirectPopup(sel){
$modal_children.hide();
$modal.find(sel).show();
}
// 关闭所有弹窗
$btn_close.on("click", function(){
// $modal.fadeOut();
resetForm();
$modal.hide();
$modal_children.hide();
})
倒计时
function startInitTimer() {
const autoTimer = window.autoTimer;
const endTime = window.endTime;
const serverTime = window.serverTime ;
window.clearInterval(autoInitTimer);
let diffTimeStamp = serverTime - Date.now();
return setInterval(() => {
let correctStamp = Date.now() + diffTimeStamp;
const needUpdate = correctStamp > endTime;
// 倒计时结束
if (needUpdate) {
window.clearInterval(autoInitTimer);
...
return;
}
// 剩余倒计时
console.log(playEndTimeStamp - correctStamp);
}, 1000);
}
自动重试
let retryCount = 0; // 已重试次数
const fetch = async () => {
try {
console.log("请求中...");
const result = await ...;
retryCount = 0;
// return result; 可选
} catch (error) {
console.log("请求失败");
if (retryCount === 0) {
console.log("重新尝试...");
retryCount = 1;
const result = await fetch();
// return result; 可选
}
console.log("重试次数用尽")
retryCount = 0;
throw error;
}
};
颜色转换
rgb转hex
function rgbaToHex(color) {
// 提取颜色值中的 RGB 分量
const matches = color.match(/\d+/g);
if (!matches) {
return null;
}
let [r, g, b] = matches;
// 将 RGB 分量转换为整数
r = parseInt(r);
g = parseInt(g);
b = parseInt(b);
// 将 RGB 分量转换为 Hex 格式
const hex = `#${((r << 16) | (g << 8) | b).toString(16).padStart(6, '0').toUpperCase()}`;
return hex;
}
hex转rgb
export const hexToRgb = function (hexColor) {
if (!hexColor) return null;
let hex = hexColor.replace("#", "");
const length = hex.length;
if (length !== 3 && length !== 6) return null;
if (length === 3) {
hex = hex
.split("")
.map((char) => char + char)
.join("");
}
const r = parseInt(hex.slice(0, 2), 16);
const g = parseInt(hex.slice(2, 4), 16);
const b = parseInt(hex.slice(4, 6), 16);
return `rgb(${r},${g},${b})`;
};
平滑滚动
CSS的 scroll-behavior: smooth
// body {
// scroll-behavior: smooth;
// }
document.body.scrollTop = 0;
JS的 scrollTo API
$ele.scrollTop({
top: 0,
behavior: "smooth"
})
requestAnimationFrame
+ 缓动函数 实现
// 以滚动window为例
// params:targetPosition目标位置 speed滚动所需时间跨度
function smoothScrollTo(targetPosition, speed = 800) {
const startPosition = window.scrollY; // 当前滚动位置
const distance = targetPosition - startPosition; // 目标距离
let startTime = null; // 记录动画开始时间
// 缓动函数(控制滚动进度的加速和减速)
function easeInOutQuad(t) {
return t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;
}
function animation(currentTime) {
if (!startTime) startTime = currentTime; // 设置动画开始时间
const timeElapsed = currentTime - startTime; // 计算已用时间
const progress = Math.min(timeElapsed / speed, 1); // 计算滚动进度,范围0到1
// 使用缓动函数和进度计算当前滚动位置
const scrollPosition = startPosition + distance * easeInOutQuad(progress);
window.scrollTo(0, scrollPosition); // 更新页面滚动位置
if (progress < 1) {
requestAnimationFrame(animation); // 未完成时继续调用下一帧
}
}
requestAnimationFrame(animation); // 开始动画
}
smoothScrollTo(2000, 1000);