先看效果:
可互动的小场景
再看代码: JS部分
script>
var rotateDiv = document.getElementById('rot');
var rotateIcons = document.getElementById('rot-icons');
var clickRotateDiv = document.getElementById('click-rot');
var angle = 0;
clickRotateDiv.onclick = function() {
angle += 60;
rotateDiv.style.transform = 'rotate(' + angle + 'deg)';
rotateIcons.style.transform = 'rotate(' + angle + 'deg)';
};
var step = 2;
var color1 = 'rgba(0,0,0,0.5)';
var color2 = 'rgba(0,0,0,0.1)';
var gradient = ' conic-gradient(';
for (var i = 0; i 360; i += step) {
var color = i % (2 * step) === 0 ? color1 : color2;
gradient += color + ' ' + i + 'deg, ';
}
gradient = gradient.slice(0, -2) + '), rgb(85 93 108)';
rotateDiv.style.background = gradient;
var toggles = document.querySelectorAll('.toggle');
var tempElement = document.querySelector('.temp');
let isAnimating = false; // 添加标志以指示动画是否处于活动状态
toggles.forEach(function(toggle) {
toggle.addEventListener('click', function() {
if (this.classList.contains('active') || isAnimating) { // 检查动画是否处于活动状态
return;
}
toggles.forEach(function(toggle) {
toggle.classList.remove('active');
});
this.classList.add('active');
var tempValue = parseFloat(tempElement.textContent);
if (this.id === 'toggle-cel') {
var celsius = Math.round((tempValue - 32) * 5 / 9);
tempElement.textContent = celsius + '°C';
} else if (this.id === 'toggle-far') {
var fahrenheit = Math.round(tempValue * 9 / 5 + 32);
tempElement.textContent = fahrenheit + '°F';
}
});
});
let currentTempF = 34; // 使用以华氏度为单位的初始温度进行初始化
function easeInOutCubic(t) {
return t.5 ? 4*t*t*t : (t-1)*(2*t-2)*(2*t-2)+1;
}
function changeTemp(element, newTemp) {
let unit = element.innerHTML.includes("F") ? "°F" : "°C";
let currentTemp = unit === "°F" ? currentTempF : Math.round((currentTempF - 32) * 5 / 9);
let finalTemp = unit === "°F" ? newTemp : Math.round((newTemp - 32) * 5 / 9);
let duration = 2000; // 动画的持续时间(毫秒)
let startTime = null;
function animate(currentTime) {
if (startTime === null) {
startTime = currentTime;
}
let elapsed = currentTime - startTime;
let progress = Math.min(elapsed / duration, 1);
progress = easeInOutCubic(progress);
let tempNow = Math.round(currentTemp + (progress * (finalTemp - currentTemp)));
element.innerHTML = `${tempNow}${unit}`;
if (progress 1) {
requestAnimationFrame(animate);
} else {
// 动画完成后更新currentTempF
currentTempF = newTemp;
isAnimating = false; // 动画完成后重置标志
}
}
isAnimating = true; // 动画开始时设置标志
requestAnimationFrame(animate);
}
window.onload = function() {
const sixths = Array.from(document.querySelectorAll('.sixths'));
let index = 0;
let temp = document.querySelector('.temp');
document.querySelector('#rot-icons').addEventListener('click', () => {
sixths[index].classList.remove('active');
index = (index + 1) % sixths.length;
sixths[index].classList.add('active');
if (index == 0 ) {
changeTemp(temp, 34);
console.log("sun")
document.querySelector('#mountains').classList.remove("snow");
document.querySelector('#mountains').classList.remove("clouds");
} else if (index == 1) {
changeTemp(temp, 27);
console.log("sunset")
document.querySelector('#mountains').classList.add("sunset");
} else if (index == 2) {
changeTemp(temp, 14);
console.log("moon")
document.querySelector('#mountains').classList.remove("sunset");
document.querySelector('#mountains').classList.add("moon");
} else if (index == 3) {
changeTemp(temp, 16);
console.log("clouds")
document.querySelector('#mountains').classList.add("clouds");
} else if (index == 4) {
changeTemp(temp, 8);
console.log("storm")
document.querySelector('#mountains').classList.add("storm");
} else if (index == 5) {
changeTemp(temp, -4);
console.log("snow")
document.querySelector('#mountains').classList.remove("moon");
document.querySelector('#mountains').classList.remove("storm");
document.querySelector('#mountains').classList.add("snow");
}
let loadingBar = document.querySelector('.loading-bar');
loadingBar.classList.add('active');
setTimeout(() => {
loadingBar.classList.remove('active');
}, 1200);
});
};
/script>
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
简述 深度优先和广度优先是在图和树的遍历搜索中比较常用的搜索方法 内容 广度优先搜索和深度优先搜索一样,都是对图进行搜索的算法,都是从起点开始顺着边搜索,此时并不知道图的整体结构,直到找到指定节点(即终点)。在此过程中每走到一个节点,就会判断一次它是否为终点;…