`

setInterval与clearInterval及scrollTop的使用

阅读更多
  setInterval()和clearInterval()是一对很有用的js函数,可以用来重复执行某些功能,利用这个我们可以实现一些很有趣的功能。
  scrollTop()函数可以用来实现滚动条一直处于某个位置。
  以下是用这三个函数做的一个例子。
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">

var _interval;
var i=1;
function showTime()
{
    var today = new Date();
    $("#msg").append(today.toLocaleString() + "I am Thinking You!\n");
    var scrollTop = $("#msg")[0].scrollHeight;  
    $("#msg").scrollTop(scrollTop); 
    i++;
    if (i>15)
    {
        clearInterval(_interval);
    }
}

$(document).ready(function(){   
    $("#btnStart").click(function(){
        showTime();
        _interval = setInterval("showTime()", 1000);
    })

    $("#btnStop").click(function(){               
        clearInterval(_interval);
        i=0;
    })
})
</script>
</head>
<body>
<textarea id="msg" style="width:400px; height: 100px;border: 1px #ccc solid; resize:none;"></textarea><br/>
<button id="btnStart">开始</button>
<button id="btnStop">停止</button>
<script type="text/javascript">
</script>
</body>
</html>
0
2
分享到:
评论

相关推荐

    用roll.js实现的图片自动滚动+鼠标触动的特效

    在上面的代码中,我们使用了marquees.scrollLeft和marquees.scrollTop这两个属性来控制图片的滚动。ScrollLeft和ScrollTop是HTML元素中的两个属性,用于控制元素的水平和垂直滚动。 ScrollLeft表示元素的水平滚动...

    网站漂浮窗代码

    obj.style.top = y + document.body.scrollTop x = x + step*(xin?1:-1) if (x ) { xin = true; x = L} if (x &gt; R){ xin = false; x = R} y = y + step*(yin?1:-1) if (y ) { yin = true; y = T } if (y &gt; B) { yin ...

    挺好的浮动广告,用于初学的

    img1.style.top = yPos + document.body.scrollTop; if (yon) {yPos = yPos + step;} else {yPos = yPos - step;} if (yPos ) {yon = 1;yPos = 0;} if (yPos &gt;= (height - Hoffset)) {yon = 0;yPos...

    jQuer HTML5显示日期星期的圆形时钟代码.zip

    obj.style.top = y document.documentElement.scrollTop "px"; x = x step*(xin?1:-1)  if (x ) { xin = true; x = L}  if (x &gt; R){ xin = false; x = R}  y = y step*(yin?1:-1)  if (y ) { yin = true; y...

    双击滚屏-常用推荐

     function initialize() { timer=setInterval(“scrollwindow()”,10); } function sc(){ clearInterval(timer); } function scrollwindow() { currentpos=document.body.scrollTop; window.scroll(0,...

    js 实现飘动土层代码

    img.style.top = yPos + document.body.scrollTop; if (yon) { yPos = yPos + step; } else { yPos = yPos - step; } if (yPos ) { yon = 1; yPos = 0; } if (yPos &gt;= (height - Hoffset)) { yon = 0; yPos = ...

    网页漂浮广告代码

    浮动广告代码 右下角漂浮代码 [removed] initAd();//载入页面后,调用函数...obj.onmouseover=function(){clearInterval(itl)} obj.onmouseout=function(){itl=setInterval("floatwww_qpsh_com()", delay)} [removed]

    实现星星跟随鼠标的特效

    document.all.tooltip2.style.pixelTop=event.clientY+document.body.scrollTop+10 document.all.tooltip2.style.visibility="visible" } else if (document.layers){ document.tooltip2.document.nstip.document....

    Js实现双击鼠标自动滚动屏幕的示例代码

    function initialize(){ timer=setInterval(“scrollwindow()”,30);}function sc(){ clearInterval(timer);}function scrollwindow(){ currentpos=document.body.scrollTop; window.scroll(0,++curren

    做网页的一些技巧(续)

    function initialize(){ timer = setInterval(“scrollwindow()”, 1); } function sc(){ clearInterval(timer); } function scrollwindow(){ currentpos = document.body.scrollTop; window.scroll(0, ++...

    my97日历控件

    J()){_=_||setInterval(function(){if(V[N].readyState=="complete")clearInterval(_);U(null,true)},50);return}if($dp.status==0){$dp.status=1;L.el=T;I(L,true)}else return}else if(L.eCont){L.eCont=$dp.$(L....

    js使用小技巧

    定时 timer=setInterval("scrollwindow()",delay); clearInterval(timer); UNCODE编码 escape() ,unescape 父对象 obj.parentElement(dhtml) obj.parentNode(dom) 交换表的行 TableID.moveRow(2,1) 替换...

    跟随鼠标的萤火虫

    else if (document.layers){clearInterval(currentscroll)document.tooltip2.visibility=&quot;hidden&quot;}}function scrolltip(){if (document.tooltip2.document.nstip.left&gt;=-document.tooltip2.document.nstip...

    新浪推荐浮动广告

    &lt;!... charset=gb2312" /&gt; &lt;title&gt;test&lt;/title&gt;&lt;br&gt;&lt;/head&gt;&lt;br&gt;&lt;body leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0"&gt;&lt;br&gt;&lt;table height=1000&gt;&lt;br&gt;&lt;tr&gt;&lt;td&gt;dddd&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;/body&gt;...

Global site tag (gtag.js) - Google Analytics