`
enix2212
  • 浏览: 25386 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

scroll base

阅读更多
开发版,水平方向已完成,垂直方向没测,可以跨浏览,滚动多个当前元素索引有问题
<!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>无标题文档</title>
<style type="text/css">
* { padding:0; margin:0;}
#scroll { width: 212px; overflow: hidden; margin-left: 100px; }
#container { height: 100px; overflow: hidden; }
#container div { width: 99px; height: 98px; float: left; margin-right: 10px; border: 1px solid #eee; overflow: hidden; }
</style>
</head>

<body>
<b id="play">play</b><b id="pause">pause</b>
<div id="scroll">
<div id="container">
	<div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div><div>7</div><div>8</div><div>9</div>
</div>
</div>
<script type="text/javascript">
function ScrollClass(O){
	if(!(this instanceof arguments.callee)) return new arguments.callee(O);
	var doc,indicator,that,config,container,scroll,toolkit,items,length,correct;
	(doc=document,indicator=arguments.callee,that=this,container=scroll=items=correct=null,config={
		S:that.S=O.S,//滚动包装
		C:that.C=O.C,//滚动容器
		D:that.D=O.D,//滚动距离 后面会智能赋值
		O:that.O=O.O || 'left',//滚动方式
		N:that.N=O.N || 1,//滚动元素的个数
		delay:that.delay=O.delay || 2000, //间隔时间
		speed:that.speed=O.speed  || 1000,
		auto:that.auto=O.auto || 'true'
	});
	'function'===typeof(O.callback) && (that.callback=O.callback);

	var toolkit={
		isEmptyO : function (O){
			for(var i in O){
				return true;	
			};
			return false;
		},
		currentStyle : function (element,property){
			var computedStyle=null;
			return undefined!==element.currentStyle ? element.currentStyle[property] : document.defaultView.getComputedStyle(element,null)[property];
		},
		each:function(object,callback){
			var index,i=0,len=object.length,isO=len===undefined || ({}).toString.call(object).slice(8,-1)==='Object';
			if (isO) {
				for (index in object) {
				  if (callback.call(object[index], index, object[index]) === false) {
					break;
				  }
				}
			} else {
				for (; i < len;) {
				  if (callback.call(object[i], i, object[i++]) === false) {
					break;
				  }
				}
			}
		}
	};
	
	var boolean=!! function(){
		that.S=scroll=doc.getElementById(config.S),that.C=container=doc.getElementById(config.C),items=container.children,length=items.length;
		var item=items.item(0),itemSize=0;
		
		if(!doc.getElementById(O.S) || !doc.getElementById(O.C) || (config.O==='left' ? container.scrollWidth*2<container.offsetWidth : container.scrollHeight*2<container.offsetHeight)) return false;
		
		correct=config.O==='left' ? 
		(
			[parseInt(toolkit.currentStyle(item,'marginLeft')),parseInt(toolkit.currentStyle(item,'marginRight'))]
		):
		(
			[parseInt(toolkit.currentStyle(item,'marginTop')),parseInt(toolkit.currentStyle(item,'marginBottom'))]
		);
		
		itemSize=(config.O==='left' ? item.offsetWidth :  item.offsetHeight)+(correct[0]>0?correct[0] : correct[1]);
		//
		!config.D && (that.D=config.D=(itemSize*config.N));
		//
		config.O==='left' && (container.style.width=(config.D)*length+'px');
		container.style.height=container.scrollHeight+'px';
		if(config.D%itemSize!==0){
			throw new Error('滚动的距离和元素的尺寸不符');
			return false;
		};
		config.O=config.O==='left'? 'scrollLeft' : 'scrollTop';
		//config.items=Array.prototype.slice.call(items);
		//that.items=([]).slice.call(items);
		config.items=[],that.items=[]		
		/*toolkit.each(items,function(a,b){
			config.items.push(b);
			that.items.push(b);
		})*/
		for(var i=0;i<items.length;i++){
			var node=items[i]
			config.items.push(node);
			that.items.push(node);
		}
		!that.scrolling && (that.scrolling=false);
		return true;
	}();
	
	if(boolean===false) return false;
	indicator.fn=indicator.prototype,indicator.fn.constructor=indicator;
	
	
	!this.Tween && (indicator.fn.Tween=function(){
		return that.Linear.apply(that,arguments);
	});
	indicator.fn.Linear=function(t,b,c,d){ return c*t/d + b; };
	indicator.prototype.fx=function(t,b,c,d){
		return Math.ceil(that.Tween.apply(that,arguments));
	};
	indicator.fn.index=function(node,items){	
		
		var index=-1;
		toolkit.each(items,function(a,b){
			if(node===b){
				index=a;
			};
		});
		return index;
	};
	indicator.fn.compute=function(O){
		var t=0,b=0,c=that.D,d=50,timeout,end;
		!that.stack && (that.stack=[0]);
		
		void function(){
			var result=that.fx(t,b,c,d);
			result<=that.D &&  (t++,that.stack.push(result),timeout=setTimeout(arguments.callee,13)); 
			return false;
		}();
	};
	indicator.fn.condition=function(){
		var indicator=arguments.callee;
		if(that.O==='left'){
			return indicator=function(){
				return that.S.scrollLeft<that.D;
			}.apply(that);
		}else{
			return indicator=function(){
				return that.S.scrollTop<that.D;
			}.apply(that);
		};
	};
	indicator.fn.action=function(){
		var k=0,len=that.stack.length,interval,fn=0,distance=0;
		that.scrolling=true;
		(k<=len) && (
			interval=setInterval(function(){	
				distance=that.stack[++k],		
				(undefined !==distance && distance<that.D)?(	
						(that.callback && fn===0) &&(fn=1,that.callback.call(that)),
						that.S.scrollLeft=distance
					) : (
						distance===that.D && (
							fn=0,
							k=0,
							clearInterval(interval),
							that.nodeControl(),
							setTimeout(function(){that.end();},that.delay)
						)
				);
			},13)
		);
		return false;
	};
	indicator.fn.nodeControl=function(){
		toolkit.each(new Array(that.N),function(a,b){
			var temp=that.items.shift();
			a<that.N && (
				that.current=that.index(temp,config.items),
				that.C.appendChild(temp),
				that.items.push(temp)
			);
		});
		//
		that.S.scrollLeft=0;
		//
	};
	indicator.fn.end=function(){
		that.scrolling=false;
	};
	indicator.fn.activity=function(){
		that.loop=setInterval(function(){
			(that.scrolling===false && that.S.scrollLeft===0) && (
				that.action()
			);
		},5);
	};
	
	indicator.fn.pause=function (){
		clearInterval(that.loop);
	};
	indicator.fn.play=function (){
		that.activity();
	};
	that.compute.call(that,that.O);
	that.auto==='true' && that.activity();
};
void function(){
	var sroll=ScrollClass({S:'scroll',C:'container',speed:2000,delay:2000,N:2,auto:'true',callback:function(){
		//console.dir(this);
	}});
	document.getElementById('play').onclick=function(){
		sroll.play();
	};
	document.getElementById('pause').onclick=function(){
		sroll.pause();
	};
}();
</script>
</body>
</html>



一年前实现的scroll类

function SlideClass(A) {
    this.index = 0;
    this.timer = null;
    this.parent = A.parent || {};
    this.distance = A.distance || 0;
    this.delay = A.delay || 2000;
    this.autoPlay = A.autoPlay || "true";
    this.counter = A.counter || "false";
    this.direction = A.direction || "left";
    this.chlid = A.child || "li";
    if (!document.getElementById(A.parent)) {
        throw new Error("包裹的父节点必须")
    }
    this.parent = document.getElementById(A.parent);
    if (this.counter) {
        this.page = document.getElementById(this.counter)
    }
    this.parent.scrollTop = 0;
    this.parent.scrollLeft = 0;
    this.sub = this.parent.getElementsByTagName(this.chlid) || {};
    this.number = this.sub.length;
    this.init.call(this)
}
SlideClass.prototype = {
    init: function () {
        if (this.autoPlay === "true") {
            this.auto.call(this)
        }
        if (this.page) {
            this.pageShow.call(this)
        }
        this.fnCall()
    },
    auto: function () {
        var A = this;
        this.stop.call(this);
        this.timer = setInterval(function () {
            A.next()
        }, this.delay)
    },
    prev: function () {
        this.play(-1)
    },
    next: function () {
        this.play(1)
    },
    fnCall: function () {},
    stop: function () {
        clearInterval(this.timer)
    },
    pageShow: function () {
        var A = this.index + 1 > this.number ? this.number : this.index + 1;
        this.page.innerHTML = A + "/" + this.number
    },
    play: function (A) {
        this.index += A;
        if (this.index == this.number) {
            this.index = 0
        }
        if (this.index < 0) {
            this.index = this.number - 1
        }
        this.fx(this.parent, this.index * this.distance)
    },
    fx: function (D, C) {
        var A = function (F, E) {
                var G = Math[F - E > 0 ? "floor" : "ceil"];
                return function () {
                    return [F += G((E - F) * 0.01), F - E]
                }
            };
        var B;
        if (this.direction === "top") {
            B = A(D.scrollTop, C);
            clearInterval(D.timer);
            D.timer = setInterval(function () {
                var E = B();
                D.scrollTop = E[0];
                if (E[1] == 0) {
                    clearInterval(D.timer)
                }
            }, 10)
        } else {
            B = A(D.scrollLeft, C);
            clearInterval(D.timer);
            D.timer = setInterval(function () {
                var E = B();
                D.scrollLeft = E[0];
                if (E[1] == 0) {
                    clearInterval(D.timer)
                }
            }, 10)
        }
        if (this.page) {
            this.pageShow.call(this)
        }
        this.fnCall.call(this)
    }
};
var slide = new SlideClass({
    parent: 'preview',
    child: 'li',
    distance: 470,
    delay: 4000,
    current: 'curr'
});
分享到:
评论

相关推荐

    android_proguard-base-6.0.3.zip

    Mac 路径为 Contents/gradle/m2repository/net/sf/proguard/proguard-base Win gradle/m2repository/net/sf/proguard/proguard-base 为了安全 先备份原来的 jar 将gradle里面的 release { minifyEnabled ...

    ListBox自动滚动到底部

    首先,确保`Scroll`事件被正确地绑定,然后在事件处理函数中设置`SelectedIndex`为最大值,即`ListBox.Items.Count - 1`。这将使ListBox选中最底部的项,从而将滚动条移动到底部。示例代码如下: ```csharp private...

    jQuery UI custom

    div显示上下滚动条的css代码 ;height:120px; overflow-y:scroll; border:1px solid;"&gt; 这里是你要显示的内容 ... scrollbar-base-color:#ff6600; border:1px solid;"&gt; 这里是你要显示的内容 &lt;/div&gt;

    div滚动条 带滚动条的div div滚动条样式

    * `scroll-bar-base-color`:设置滚动条的主要颜色。 总结 本篇文章详细介绍了 DIV 滚动条的样式设置,包括如何实现 DIV 滚动条、DIV 滚动条的样式设置和滚动条颜色属性。通过设置 `overflow` 属性和滚动条颜色...

    DataGridView固定滚动条

    ScrollEventArgs se = new ScrollEventArgs(ScrollEventType.EndScroll, _scrollPosition, VerticalScroll.Visible, VerticalScroll.LargeChange, VerticalScroll.SmallChange); OnScroll(se); } // 临时禁用...

    使用CSS样式position:fixed水平滚动的方法

    base.options = $.extend({}, $.ScrollFixed.defaultOptions, options); $(window).resize(windowResize); $(window).scroll(windowScroll); _fix_position(); console.log(base.options.fixed); }; base....

    C# winform 重绘滚动条

    为了实现滚动条的动态变化,例如滑块的移动,还需要处理Scroll事件。在事件处理函数中,根据滚动条的状态更新滑块的位置,并重新绘制控件: ```csharp protected override void OnScroll(ScrollEventArgs se) { ...

    为RichTextBox控件设置自定义滚动条

    base.OnScroll(se); // 添加自定义滚动逻辑 } ``` 6. **自定义绘制滚动条** - 虽然.NET Framework内置的`RichTextBox`控件不支持直接自定义滚动条外观,但可以通过继承`RichTextBox`并重写`OnPaint`方法,利用...

    开发备必——WEB前端开发规范文档

    17. 滚动条:scroll 18. 内容:content 19. 标签页:tab 20. 文章列表:list 21. 提示信息:msg 22. 小技巧:tips 23. 栏目标题:title 24. 链接:links 25. 页脚:footer 26. 服务:service 27. 热点:hot 28. 新闻...

    UGUI ScrollRect 居中

    base.OnValueChanged(value); // 计算内容区域的大小与视口大小的比例 float contentWidthRatio = content.rectTransform.sizeDelta.x / viewport.rect.size.x; float contentHeightRatio = content.rect...

    滚动条的应用(C#代码编写)

    base.OnResize(e); // 计算滚动条的最大值,比如根据列表框的项目数量 ListBox listBox = FindControl("listBox1"); // 假设有一个名为listBox1的列表框 scrollBar.Maximum = listBox.Items.Count; } ``` ### ...

    android 小知识积累

    if (scrollState == SCROLL_STATE_IDLE) { // SCROLL_STATE_IDLE 表示已经停止滚动 // 更新适配器数据 adapter.notifyDataSetChanged(); // 设置ListView的当前位置 listView.setSelection(lastItem - 1); } ...

    给groupbox加上滚动条

    private void ScrollViewer_Scroll(object sender, ScrollEventArgs e) { // 更新内部控件的位置... } ``` 通过以上步骤,我们就成功地为`GroupBox`添加了滚动条功能。这个项目中的代码示例应该包含这些关键部分,...

    C# winform自定义滚动条

    1. **事件处理**:滚动条的常见事件包括`Scroll`、`ValueChanged`和`DragScroll`。通过重写这些事件的处理函数,可以控制滚动条的行为,比如在用户滚动时执行特定操作。 2. **自定义滑块(Thumb)**:滑块是滚动条...

    Unity使用ScrollRect制作摇杆

    base.OnDrag(eventData); var contentPosition = this.content.anchoredPosition; // 限制摇杆移动范围在半径内 if (contentPosition.magnitude &gt; mRadius) { contentPosition = contentPosition....

    MTK_MMI资料

    DM_BASE_LAYER_START, DM_NEW_LAYER_START, DM_WALLPAPER, DM_CIRCULAR_MENU1, DM_LIST1, DM_DYNAMIC_LIST1, DM_ASYNCDYNAMIC_LIST1, DM_MATRIX_MENU1, DM_MULTILINE_INPUTBOX1, DM_EMS_INPUTBOX1, DM_DIALER_INPUT_...

    一个支持annotation的SSH整合示例项目

    卷 (USB)Project 的文件夹 PATH 列表 卷序列号码为 0006EE44 CCBE:F425 I:. │ .project │ pom.xml │ pom.xml~ │ text.txt │ ├─.settings │ org.maven.ide.eclipse.prefs ...│ │ org.eclipse.wst.common....

    RichTextBox添加行号.zip

    3. **处理Scroll事件**:当`RichTextBox`滚动时,我们需要更新行号的显示。我们需要监听`Scroll`事件,根据滚动条的位置调整行号的可见部分。 ```csharp private void richTextBox_Scroll(object sender, ...

    C#自定义控件实例---实现带行数和标尺的RichTextBox(201903)

    base.OnPaint(e); // 计算行数 int lineCount = this.GetFirstVisibleLine() + this.Lines.Length; // 绘制行号区域 using (SolidBrush brush = new SolidBrush(this.ForeColor)) { for (int i = this....

Global site tag (gtag.js) - Google Analytics