Jquery與JS兩種方法仿twitter/新浪微博 高度自適應無縫滾動實現代碼_jquery

來源:互聯網
上載者:User

首先是Jquery

<!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> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> <script language="javascript"> $(function(){ var scrtime; $("#con").hover(function(){ clearInterval(scrtime); },function(){ scrtime = setInterval(function(){ var $ul = $("#con ul"); var liHeight = $ul.find("li:last").height(); $ul.animate({marginTop : liHeight+40 +"px"},1000,function(){ $ul.find("li:last").prependTo($ul) $ul.find("li:first").hide(); $ul.css({marginTop:0}); $ul.find("li:first").fadeIn(1000); }); },3000); }).trigger("mouseleave"); }); </script> <style type="text/css"> <!-- *{ margin:0; padding:0;} ul,li{ list-style-type:none;} body{ font-size:13px; background-color:#999999;} #con{ width:700px; height:400px; margin:10px auto; position:relative; border:1px #666 solid; background-color:#FFFFFF; overflow:hidden;} #con ul{ position:absolute; margin:10px; top:0; left:0; padding:0;} #con ul li{ width:100%; border-bottom:1px #333333 dotted; padding:20px 0; overflow: } #con ul li a{ float:left; border:1px #333333 solid; padding:2px;} #con ul li p{ margin-left:68px;line-height:1.5; padding:10px;} --> </style> <body> <div id="con"> <ul> <li> <p class="vright">第一最好不相見,如此便可不相戀。第二最好不相知,如此便可不相思。 第三最好不相伴,如此便可不相欠。第四最好不相惜,如此便可不相憶。 作相思。 </p> </li> <li> <p class="vright">第一最好不相見,如此便可不相戀。第二最好不相知,如此便可不相思。 第三最好不相伴,如此便可不相欠。第四最好不相惜,如此便可不相憶。 第五最好不相愛, 不相對,如此便可不相會。 第七最好不相誤,如此便可不相負。第八最好不相許,如此便可不相續。 第九最好不相依,如此便可不相偎。第十最好不相遇,如此便可不相聚。 但曾相見便相知,相見何如不見時。安得與君相訣絕,免教生死作相思。 </p> </li> <li> <p class="vright">第一最好不相見,如此便可不相戀。第二最好不相知,如此便可不相思。 第三最好不相伴,如此便可不相欠。第四最好不相惜,如此便可不相憶。 第五最好不相愛,如此便可不相棄。 第六最好不相對,如此便可不相會。 第七最好不相誤,如此便可不相負。第八最好不相許,如此便可不相續。 第九最好不相依,如此便可不相偎。第十最好不相遇,如此便可不相聚。 但曾相見便相知,相見何如不見時。安得與君相訣絕,免教生死作相思。 </p> </li> <li> <p class="vright">第一最好不相見,如此便可不相戀。第二最好不相知,如此便可不相思。 第三最好不相伴,如此便可不相欠。第四最好不相惜,如此便可不相憶。 第五最好不相愛,如此便可不相棄。 第六最好不相對,如此便可不相會。 第七最好不相誤,如此便可不相負。第八最好不相許,如此便可不相續。 第九最好不相依,如此便可不相偎。第十最好不相遇,如此便可不相聚。 但曾相見便相知,相見何如不見時。安得與君相訣絕,免教生死作相思。 </p> </li> </ul> </div> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

這個是JavaScript:
<!doctype html> <html> <head> <title>tweets-slide</title> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <style type="text/css"> ul, li {margin:0; padding:0;list-style:none} body { margin: 0; height: 100%; background: #333; } .wp { position: relative; width: 800px; height: 400px; overflow: hidden; margin: 20px auto; border: 4px solid #121212; background: #fff; } .slider { position: absolute; width: 760px; padding: 0 20px; left:0; top: 0; } .fl {float:left} .slider img {display:block; padding: 2px; border: 1px solid #ccc} .slider li {padding: 20px 0; border-bottom: 1px dashed #ccc;overflow:hidden;width:100%} .slider p {font-size: 12px;margin:0;padding-left:68px;color:#333;line-height:20px;} </style> <script type="text/javascript"> function H$(i) {return document.getElementById(i)} function H$(c, p) {return p.getElementsByTagName(c)} var slider = function () { function init (o) { this.id = o.id; this.at = o.auto ? o.auto : 3; this.o = 0; this.pos(); } init.prototype = { pos : function () { clearInterval(this.__b); this.o = 0; var el = H$(this.id), li = H$('li', el), l = li.length; var _t = li[l-1].offsetHeight; var cl = li[l-1].cloneNode(true); cl.style.opacity = 0; cl.style.filter = 'alpha(opacity=0)'; el.insertBefore(cl, el.firstChild); el.style.top = -_t + 'px'; this.anim(); }, anim : function () { var _this = this; this.__a = setInterval(function(){_this.animH()}, 20); }, animH : function () { var _t = parseInt(H$(this.id).style.top), _this = this; if (_t >= -1) { clearInterval(this.__a); H$(this.id).style.top = 0; var list = H$('li',H$(this.id)); H$(this.id).removeChild(list[list.length-1]); this.__c = setInterval(function(){_this.animO()}, 20); //this.auto(); }else { var __t = Math.abs(_t) - Math.ceil(Math.abs(_t)*.07); H$(this.id).style.top = -__t + 'px'; } }, animO : function () { this.o += 2; if (this.o == 100) { clearInterval(this.__c); H$('li',H$(this.id))[0].style.opacity = 1; H$('li',H$(this.id))[0].style.filter = 'alpha(opacity=100)'; this.auto(); }else { H$('li',H$(this.id))[0].style.opacity = this.o/100; H$('li',H$(this.id))[0].style.filter = 'alpha(opacity='+this.o+')'; } }, auto : function () { var _this = this; this.__b = setInterval(function(){_this.pos()}, this.at*1000); } } return init; }(); </script> </head> <body> <div class="wp"> <ul id="slider" class="slider"> <li> <p>曾慮多情損梵行    入山又恐別傾城    世間安得雙全法    不負如來不負卿</p> </li> <li> <p>第一最好不相見,如此便可不相戀。 第二最好不相知,如此便可不相思。 第三最好不相伴,如此便可不相欠。 第四最好不相惜,如此便可不相憶。 第五最好不相愛,如此便可不相棄。 第六最好不相對,如此便可不相會。 第七最好不相誤,如此便可不相負。 第八最好不相許,如此便可不相續。 第九最好不相依,如此便可不相偎。 第十最好不相遇,如此便可不相聚。 但曾相見便相知,相見何如不見時。 安得與君相訣絕,免教生死作相思。</p> </li> <li> <p>那一天 閉目在經殿香霧中 驀然聽見你頌經中的真言 那一月 我搖動所有的轉經筒 不為超度 只為觸摸你的指尖 那一年 磕長頭匍匐在山路 不為覲見 只為貼著你的溫暖 那一世 轉山轉水轉佛塔啊 不為修來生 只為途中與你相見 只是 就在那一夜 我忘卻了所有 拋卻了信仰 捨棄了輪迴 只為那 曾在佛前哭泣的玫瑰 早已失去舊日的光澤 </p> </li> <li> <p>班紮古魯白瑪的沉默      你見 或者不見我      我就在那裡      不悲不喜           你念 或者不念我      情就在那裡      不來不去           你愛 或者不愛我      愛就在那裡      不增不減           你跟 或者不跟我      我的手就在你手裡      不舍不棄           來我的懷裡      或者      讓我住進你的心間      默然 相愛      寂靜 歡喜 </p> </li> </ul> </div> <script type="text/javascript"> new slider({id:'slider'}) </script> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

兩種代碼比較,很顯然,Jquery的代碼簡單明了,強力推薦!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.