当前位置:   article > 正文

分享一个可交互的小场景(二)

分享一个可交互的小场景(二)

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站

先看效果:

可互动的小场景

再看代码: 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/81545
推荐阅读
相关标签
  

闽ICP备14008679号