JQuery實(shí)現(xiàn)左右滾動(dòng)菜單特效
經(jīng)過了半天的時(shí)間,這個(gè)使用JQuery開發(fā)出來的左右滾動(dòng)菜單功能也算是完成了,暫時(shí)還沒有發(fā)現(xiàn)錯(cuò)誤的現(xiàn)象?,F(xiàn)在把代碼完整的代碼拿出來分享!
scrollable.js
JQuery左右滾動(dòng)菜單特效腳本代碼引用片段:
scrollable = function(content, render, options, beforeScroll) { /* * @author: selfimpr * @blog: http://blog.csdn.net/lgg201 * @e-mail: lgg860911@yahoo.com.cn * * 注意: * 1. content必須自己指定寬度. 如果其中的元素使用塊元素, 請(qǐng)使用float: left向左浮動(dòng). * 2. 使用時(shí), 盡量自定義樣式, 由于本人水平欠佳, 不能作出更加通用的東西, 呵呵. * * 參數(shù)解釋 * content: 內(nèi)容元素, 可以是選擇器或JQUERY封裝的DOM元素 * render: 渲染到的目標(biāo)容器, 可以是選擇器或JQUERY封裝的DOM元素 * options: 選項(xiàng) * scrollable_class: 整體scrollable的外框架樣式 , 默認(rèn): ui-scrollable * scrollable_left_class: 左按鈕的樣式, 默認(rèn): ui-scrollable-left * scrollable_container_class: 內(nèi)容容器的樣式, 默認(rèn): ui-scrollable-container * scrollable_right_class: 右按鈕的樣式, 默認(rèn): ui-scrollable-right * delay: 鼠標(biāo)放上或點(diǎn)擊按鈕時(shí)兩次移動(dòng)之間的時(shí)間間隔, 整數(shù) * speed: 鼠標(biāo)放上按鈕時(shí), 一次移動(dòng)的距離, 整數(shù) * speedup: 鼠標(biāo)點(diǎn)下按鈕時(shí), 一次移動(dòng)的距離, 整數(shù) * resizeEvent: 是否監(jiān)聽窗口改變大小的事件, 布爾值, * 監(jiān)聽窗口改變大小時(shí), 在刷新頁面后, 感覺顯示有點(diǎn)別扭, 所以默認(rèn)了false * beforeScroll: 內(nèi)容滾動(dòng)時(shí)候的事件回調(diào)方法. * 接受參數(shù)(兩個(gè)對(duì)象): 第一個(gè)是滾動(dòng)前內(nèi)容左右位置, 第二個(gè)是滾動(dòng)后內(nèi)容左右位置. * 注意: 該事件可以使內(nèi)容不受邊界限制的滾動(dòng). */ options.scrollable_class = options.scrollable_class || 'ui-scrollable'; options.scrollable_left_class = options.scrollable_left_class || 'ui-scrollable-left'; options.scrollable_container_class = options.scrollable_container_class || 'ui-scrollable-container'; options.scrollable_right_class = options.scrollable_right_class || 'ui-scrollable-right'; options.leftText = options.leftText || ''; options.rightText = options.rightText || ''; options.delay = options.delay || 20; options.speed = options.speed || 5; options.speedup = options.speedup || 10; options.resizeEvent = options.resizeEvent || false; var render = (typeof render == 'string' ? $(render) : render); var content = (typeof content == 'string' ? $(content) : content); var scrollable = $('<div></div>') .attr('id', 'scrollable_' + content.attr('id')) .attr('className', options.scrollable_class); var left = $('<div></div>') .attr('id', 'scrollable_left_' + content.attr('id')) .attr('className', options.scrollable_left_class); left.text(options.leftText); var container = $('<div></div>') .attr('id', 'scrollable_container_' + content.attr('id')) .attr('className', options.scrollable_container_class); content.css('line-height', '29px') .css('position', 'relative') .css('left', '0px') .css('overflow', 'hidden') .css('float', 'left'); var right = $('<div></div>') .attr('id', 'scrollable_right_' + content.attr('id')) .attr('className', options.scrollable_right_class); right.text(options.rightText); show = function() { scrollable.appendTo(render); container.appendTo(scrollable); left.css('display', ''); right.css('display', ''); content.appendTo(container); left.prependTo(scrollable); right.appendTo(scrollable); if(content.width() <= container.width() + 20) { scrollable.remove('.' + options.scrollable_left_class); scrollable.remove('.' + options.scrollable_right_class); left.css('display', 'none'); right.css('display', 'none'); container.width(content.width()); scrollable.width(container.width()); } container.position = {left: container.css('left').substr(0, -2)} container.position.right = container.position.left + container.width(); content.position = {left: new Number(content.css('left').substr(0, -2))} content.position.right = content.position.left + content.width(); }; show(); var originalBroswerWidth = document.body.clientWidth; window.onresize = function() { if(options.resizeEvent) { var newBroswerWidth = document.body.clientWidth; var percent = newBroswerWidth / originalBroswerWidth; container.width(container.width() * percent); scrollable.width(container.width() + left.width() + right.width()); show(); } originalBroswerWidth = document.body.clientWidth; } var scroll = false; move = function(distance) { var newLeft = content.position.left + distance; var newRight = content.position.right + distance; if(distance > 0 && newLeft > container.position.left) { distance = container.position.left - content.position.left; scroll = false; } else if(distance < 0 && newRight < container.position.right) { distance = content.position.right - container.position.right; scroll = false; } newLeft = content.position.left + distance; newRight = content.position.right + distance; scorll = beforeScroll ? beforeScroll( {left: content.position.left, right: content.position.right}, {left: newLeft, right: newRight}) : scroll; if(scroll) { content.css('left', newLeft + 'px'); content.position.left += distance; content.position.right += distance; setTimeout('move(' + distance + ')', options.delay); } } left.mouseover(function() { scroll = true; move(options.speed); }); right.mouseover(function() { scroll = true; move(-options.speed); }); left.mouseout(function() { scroll = false; }); right.mouseout(function() { scroll = false; }); left.mousedown(function() { scroll = true; move(options.speedup); }); right.mousedown(function() { scroll = true; move(-options.speedup); }); left.mouseup(function() { scroll = false; }); right.mouseup(function() { scroll = false; }); }
Default.aspx
JQuery左右滾動(dòng)菜單特效頁面代碼引用片段:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JQuery左右滾動(dòng)菜單特效</title> <script language="javascript" type="text/javascript" src="jquery-1.4.2.min.js"></script> <script language="javascript" type="text/javascript" src="scrollable.js"></script> <style type="text/css"> .scrollable-render{} .button{cursor: hand;} .button:hover > * {background-position: 0 -42px;} .button_left{float: left; background: url(menu_out_left.gif) no-repeat 0 0; width: 4px; height: 26px;} .button_center{float: left; background: url(menu_out_bj.gif) repeat-x 0 0; width: 80px; text-align: center} .button_right{float: left; background: url(menu_out_right.gif) no-repeat 0 0; width: 4px; height: 26px;} .ui-scrollable{width: 800px; height: 29px;} .ui-scrollable-container{float: left; width: 780px; height: inherit; position: relative; overflow: hidden; border-bottom: 1px solid #DDDDDD;} .ui-scrollable-content{float: left; width: 1770px; height: inherit;} .ui-scrollable-left{float: left; background: url(scrollable_left_out.gif) no-repeat 0 0; width: 10px; height:29px; cursor: hand;} .ui-scrollable-right{float: left; background: url(scrollable_right_out.gif) no-repeat 0 0; width: 10px; height:29px; cursor: hand;} .ui-scrollable-left:hover{ float: left; background: url(scrollable_left_on.gif) no-repeat 0 0; width: 10px; height:29px; cursor: hand;} .ui-scrollable-right:hover{float: left; background: url(scrollable_right_on.gif) no-repeat 0 0; width: 10px; height:29px; cursor: hand;} </style> <script type="text/javascript"> $(function() { scrollable('#scrollable_content', '#scrollable_render', { }, function(originalPosition, newPosition) { return true; }); }); </script> </head> <body> <center> <div id="scrollable_render" class="scrollable-render"></div> <div id="scrollable_content" class="ui-scrollable-content"> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單一</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單二</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單三</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單四</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單五</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單六</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單七</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單八</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單九</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單十</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單一</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單二</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單三</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單四</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單五</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單六</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單七</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單八</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單九</div> <div class="button_right"></div> </div> <div class="button"> <div class="button_left"></div> <div class="button_center">菜單十</div> <div class="button_right"></div> </div> </div> </center> </body> </html>
當(dāng)然,我們還需要引用JQuery框架文件,我這里用的是jquery-1.4.2.min.js,自己可以在網(wǎng)上搜索下載,我就不上傳到這里了。整個(gè)JQuery左右滾動(dòng)菜單特效就是這個(gè)樣子了,自己覺得還行,希望能幫到一些有需要的朋友。
- jquery toolbar與網(wǎng)頁浮動(dòng)工具條具體實(shí)現(xiàn)代碼
- jQuery實(shí)現(xiàn)固定在網(wǎng)頁頂部的菜單效果代碼
- jquery固定底網(wǎng)站底部菜單效果
- jQuery 菜單隨滾條改為以定位方式(固定要瀏覽器頂部)
- jQuery實(shí)現(xiàn)有動(dòng)畫淡出效果的二級(jí)折疊菜單代碼
- 基于jQuery實(shí)現(xiàn)的菜單切換效果
- jquery實(shí)現(xiàn)的簡單二級(jí)菜單效果代碼
- jQuery實(shí)現(xiàn)Tab菜單滾動(dòng)切換的方法
- jQuery實(shí)現(xiàn)可用于博客的動(dòng)態(tài)滑動(dòng)菜單完整實(shí)例
- jquery實(shí)現(xiàn)仿Flash的橫向滑動(dòng)菜單效果代碼
- jQuery實(shí)現(xiàn)可關(guān)閉固定于底(頂)部的工具條菜單效果
相關(guān)文章
jQuery實(shí)現(xiàn)的手動(dòng)拖動(dòng)控制進(jìn)度條效果示例【測(cè)試可用】
這篇文章主要介紹了jQuery實(shí)現(xiàn)的手動(dòng)拖動(dòng)控制進(jìn)度條效果,涉及jQuery事件響應(yīng)及頁面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-04-04jQuery基礎(chǔ)學(xué)習(xí)技巧總結(jié)
jQuery基礎(chǔ)學(xué)習(xí)技巧總結(jié)...2007-06-06基于jquery實(shí)現(xiàn)圖片相關(guān)操作(重繪、獲取尺寸、調(diào)整大小、縮放)
這篇文章主要介紹了基于jquery實(shí)現(xiàn)圖片相關(guān)操作,包括圖片重繪、圖片獲取尺寸、圖片調(diào)整大小、圖片縮放,感興趣的小伙伴們可以參考一下2015-12-12JQuery與Ajax常用代碼實(shí)現(xiàn)對(duì)比
JQuery與Ajax常用代碼實(shí)現(xiàn)對(duì)比,大家可以看下,根據(jù)實(shí)際情況選用。2009-10-10bootstrap table實(shí)現(xiàn)iview固定列的效果實(shí)例代碼詳解
這篇文章主要介紹了bootstrap table實(shí)現(xiàn)iview固定列的效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09基于jQuery的輸入框在光標(biāo)位置插入內(nèi)容, 并選中
基于jQuery的輸入框在光標(biāo)位置插入內(nèi)容, 并選中功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-10-10jquery中實(shí)現(xiàn)標(biāo)簽切換效果的代碼
現(xiàn)在比較流行jquery插件,所以既然用了jquery那么就要用好,也不用大量的寫代碼了。2011-03-03