JS上下无缝滚动
html代码
<? xml version="1.0" encoding="UTF-8" ?> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns="http://www.w3.org/1999/xhtml"> < head > < meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> < title >scrollTop测试示例</ title > < style > <!-- #out { width:200px; height:80px; border:solid 1px blue; overflow:hidden; } #in ul { width: 100%; } #in ul li { width: 60%; float: left; overflow: hidden; list-style: none; } --> </ style > </ head > < body > < div id="out"> < div id="in"> < ul > < li >第1行</ li > < li >第2行</ li > < li >第3行</ li > < li >第4行</ li > < li >第5行</ li > </ ul > < div style="clear: both;"></ div > </ div > </ div > </ body > </ html > |
Js代码
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 | <script type= "text/javascript" > var $ = function (id) { return "string" == typeof id ? document.getElementById(id) : id; }; // 实例化一个对象并调用对象的initialize方法 var Class = { create : function () { return function () { this .initialize.apply( this , arguments); }; } }; // 为Object添加一个extend方法 Object.extend = function (destination, source) { for ( var property in source) { destination[property] = source[property]; } return destination; }; // 为对象注册事件 var addEventHandler = function (oTarget, sEventType, fnHandler) { if (oTarget.addEventListener) { oTarget.addEventListener(sEventType, fnHandler, false ); } else if (oTarget.attachEvent) { oTarget.attachEvent( "on" + sEventType, fnHandler); } else { oTarget[ "on" + sEventType] = fnHandler; } }; var Scroll = Class.create(); Scroll.prototype = { initialize : function (outObj, inObj, options) { var oScroll = this ; var iOut = $(outObj); var iIn = $(inObj); this .outHeight = iOut.offsetHeight; this .inHeight = iIn.offsetHeight; if ( this .outHeight >= this .inHeight) return ; iOut.style.overflow = "hidden" ; iIn.appendChild(iIn.cloneNode( true )); this .setOptions(options); this .outObj = iOut; this .timer = null ; this .side = 1; // 1:向上;2:向下 switch ( this .options.side) { case "down" : this .side = -1; break ; case "up" : default : this .side = 1; } addEventHandler(iIn, "mouseover" , function () {oScroll.stop();}); addEventHandler(iIn, "mouseout" , function () {oScroll.start();}); this .start(); }, setOptions : function (options) { this .options = { step : 1, // 每次滚动的px量 side : "up" , // 滚动的方向 time : 10 // 滚动的间隔时间(滚动速度) }; Object.extend( this .options, options || {}); }, scroll : function () { var inHeight = this .inHeight, outHeight = this .outHeight, iStep = this .options.step * this .side, time = this .options.time; var iScrollTop = this .outObj.scrollTop; if (iScrollTop >= (inHeight * 2 - outHeight)) { iScrollTop -= inHeight; } else if (iScrollTop <= 0) { iScrollTop += inHeight; } this .outObj.scrollTop = iScrollTop + iStep; var oScroll = this ; this .timer = setTimeout( function () {oScroll.scroll();}, time); }, start : function () { this .scroll(); }, stop : function () { clearTimeout( this .timer); } }; window.onload = function () { new Scroll( "out" , "in" , {step : 1, side : "up" , time : 20}); }; </script> |
作者:【唐】三三
出处:https://www.cnblogs.com/tangge/archive/2011/04/27/2030757.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具