Nodejs环境下javascript实现点击按钮向上向下是否缓动的效果(以后的操作dom库慢慢的完善,结合Nodejs能力探讨)

Nodejs环境下javascript实现点击按钮向上向下是否缓动的效果(以后的操作dom库慢慢的完善,结合Nodejs能力探讨)

void function(){ var Yound = { /** 设置滚动的值

  • value (string) 值
  • return (undefined) 无 / setScroll:function(value){ var con = document.documentElement.scrollTop ? document.documentElement : document.body; con.scrollTop = value; }, / 设置滚动的值
  • return (number) 返回滚动条当前的滚动高度 / getScroll:function(){ return (document.documentElement.scrollTop || document.body.scrollTop) }, /
  • els (node) 绑定点击的元素的事件
  • fx (string) 代表滚动的方向 可写值 bottom 和 top
  • isHd(boolean) 是否缓动 true 代表是 false 代表不是
  • return (undefined) **/ scroll:function(els,fx,isHd){
    els = (typeof els === “string”) ? document.getElementById(els) : els; els.onclick = function(){ var scrollTop = getScroll(); if(fx === “bottom”){ //加 if(isHd){ var time = setInterval(function(){ scrollTop += 10; setScroll(scrollTop); if(getScroll() >= (document.documentElement.scrollHeight - document.documentElement.clientHeight )){ clearInterval(time); return; }

                	 	 },1000 / 30);
                	 }else{
                	 	 setScroll(document.documentElement.scrollHeight - document.documentElement.clientHeight );
                	 }

                            
                }
                else if(fx === "top"){
                     //减
                     if(isHd){

                     	  var time = setInterval(function(){
                     	  	 scrollTop -= 10;                  	
                     	  	 if(getScroll() <= 0){
    
                     	  	 	 clearInterval(time);
                     	  	     return;
                     	  	 }
                     	  	   	 setScroll(scrollTop);
                     	  	 

                     	  },1000 / 30); 
                     }else{
                     	  setScroll(0);

                     }


                }
      	 } 
      }

  };
  for(var k in Yound){
  	 window[k] = Yound[k];
  }

}();

调用

window.onload = function(){scroll(“dd”,“bottom”,true)}


2 回复

好的,让我们根据你提供的标题和内容,创建一个完整的示例来展示如何在 Node.js 环境下使用 JavaScript 实现点击按钮时页面向上或向下滚动时的缓动效果。

示例代码

// 定义一个名为Yound的对象
var Yound = {
    // 设置滚动的值
    setScroll: function(value) {
        var con = document.documentElement.scrollTop ? document.documentElement : document.body;
        con.scrollTop = value;
    },

    // 获取滚动条当前的滚动高度
    getScroll: function() {
        return (document.documentElement.scrollTop || document.body.scrollTop);
    },

    // 绑定点击事件,实现向上或向下滚动的缓动效果
    scroll: function(els, fx, isHd) {
        els = (typeof els === 'string') ? document.getElementById(els) : els;
        
        els.onclick = function() {
            var scrollTop = Yound.getScroll();

            if (fx === "bottom") {
                // 向下滚动
                if (isHd) {
                    var time = setInterval(function() {
                        scrollTop += 10;
                        Yound.setScroll(scrollTop);
                        
                        if (Yound.getScroll() >= (document.documentElement.scrollHeight - document.documentElement.clientHeight)) {
                            clearInterval(time);
                            return;
                        }
                    }, 1000 / 30);
                } else {
                    Yound.setScroll(document.documentElement.scrollHeight - document.documentElement.clientHeight);
                }
            } else if (fx === "top") {
                // 向上滚动
                if (isHd) {
                    var time = setInterval(function() {
                        scrollTop -= 10;
                        Yound.setScroll(scrollTop);
                        
                        if (Yound.getScroll() <= 0) {
                            clearInterval(time);
                            return;
                        }
                    }, 1000 / 30);
                } else {
                    Yound.setScroll(0);
                }
            }
        }
    }
};

// 将Yound对象的方法挂载到全局对象window上
for (var k in Yound) {
    window[k] = Yound[k];
}

// 页面加载完成后调用scroll方法
window.onload = function() {
    // 例如,绑定id为"scrollButton"的按钮,使其点击后向下滚动并带有缓动效果
    Yound.scroll("scrollButton", "bottom", true);
};

解释

  1. 定义Yound对象

    • setScroll 方法用于设置滚动条的位置。
    • getScroll 方法用于获取当前滚动条的位置。
    • scroll 方法用于绑定点击事件,并根据传入参数决定滚动方向和是否使用缓动效果。
  2. 缓动效果

    • 缓动效果通过 setInterval 实现,每隔一段时间(1000/30毫秒)更新一次滚动位置。
    • 当滚动到达目标位置时,使用 clearInterval 清除定时器。
  3. 调用示例

    • window.onload 事件中调用 Yound.scroll 方法,绑定一个按钮的点击事件。当按钮被点击时,页面将向下滚动并带有缓动效果。

这样,你就可以在 Node.js 环境下实现点击按钮时页面向上或向下滚动时的缓动效果了。


在Node.js环境中,直接操作DOM是不可行的,因为Node.js主要用于服务器端开发。不过,我们可以在浏览器环境中使用JavaScript来实现点击按钮时页面滚动的缓动效果。以下是一个简单的示例代码,展示如何通过JavaScript实现向上向下滚动时的缓动效果。

示例代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Scroll Animation Example</title>
<style>
  body {
    height: 2000px;
  }
  button {
    position: fixed;
    top: 50px;
    left: 50%;
    transform: translateX(-50%);
  }
</style>
</head>
<body>
<button id="scroll-down">Scroll Down</button>
<button id="scroll-up">Scroll Up</button>

<script>
  const setScroll = (value) => {
    document.documentElement.scrollTop = value;
  };

  const getScroll = () => {
    return document.documentElement.scrollTop || document.body.scrollTop;
  };

  const scrollToPosition = (targetPosition, duration) => {
    let start = getScroll();
    let change = targetPosition - start;
    let currentTime = 0;
    const easeInOutQuad = (t, b, c, d) => {
      t /= d / 2;
      if (t < 1) return c / 2 * t * t + b;
      t--;
      return -c / 2 * (t * (t - 2) - 1) + b;
    };

    const animateScroll = () => {
      currentTime += 16;
      const val = easeInOutQuad(currentTime, start, change, duration);
      setScroll(val);
      if (currentTime < duration) {
        requestAnimationFrame(animateScroll);
      }
    };

    animateScroll();
  };

  document.getElementById('scroll-down').addEventListener('click', () => {
    scrollToPosition(document.documentElement.scrollHeight, 500);
  });

  document.getElementById('scroll-up').addEventListener('click', () => {
    scrollToPosition(0, 500);
  });
</script>
</body>
</html>

解释

  1. HTML 结构

    • 两个按钮,分别用于向下和向上滚动。
  2. CSS 样式

    • body 设置了较高的高度,以便有足够长的滚动距离。
    • 按钮固定在页面顶部中央位置。
  3. JavaScript 逻辑

    • setScrollgetScroll 分别设置和获取当前的滚动位置。
    • scrollToPosition 函数实现了缓动效果,通过 requestAnimationFrame 进行动画处理。
    • easeInOutQuad 是一个常见的缓动函数,提供了平滑过渡的效果。
    • 按钮点击事件绑定到相应的滚动操作。

这样,当用户点击按钮时,页面将平滑地滚动到指定位置。

回到顶部