Thiet Ke Web - Thiết kế web - Thiet ke website - Thiết kế website - Nhat Quang Informatics

 

Thiet Ke Web Pro.vn: Thiết Kế Web | Trang chủ Hướng Dẫn Thiết Kế Website Tùy biến thanh scroll với jQuery trong thiết kế website

Tùy biến thanh scroll với jQuery trong thiết kế website

Malihu, admin website manos.malihu.gr giới thiệu tới các bạn đoạn mã jQuery giúp tùy biến thanh scrollbar của trình duyệt. Bạn có thể tạo thanh scrollbar của riêng mình và vẫn có thể hỗ trợ cuộn chuột.

scrollbar custom, thiet ke web 

Ngoài jQuery, tác giả sử dụng jQuery UIBrandon Aaron jquery mousewheel plugin (giúp xử lý việc cuộn chuột). Bạn có thể dễ dàng tùy biến và thiết kế lại thanh scrollbar bằng CSS

Cách sử dụng

Trước tiên bạn cần tải về jQuery 1.4,jQuery UI 1.8Brandon Aaron jquery mousewheel plugin, sau đó thêm vào phần header của trang web. Nhớ thay đổi đường dẫn theo thư mục bạn chứa những file trên.

<script type="text/javascript" src="/js/jquery.min.js"></script>
<script src="/js/jquery-ui.min.js"></script>
<script type="text/javascript" src="/js/jquery.mousewheel.min.js"></script>

Mã CSS và HTML như sau

#outer_container{margin:60px; width:260px; padding:0 10px; border-top:1px solid #666; border-bottom:1px solid #666;}
#customScrollBox{position:relative; height:600px; overflow:hidden;}
#customScrollBox .container{position:relative; width:240px; top:0; float:left;}
#customScrollBox .content{clear:both;}
#customScrollBox .content p{padding:10px 5px; margin:10px 0; color:#fff; font-family:Verdana, Geneva, sans-serif; font-size:13px;}
#customScrollBox img{border:5px solid #fff;}
#dragger_container{position:relative; width:0px; height:580px; float:left; margin:10px 0 0 10px; border-left:1px solid #000; border-right:1px solid #555;}
#dragger{position:absolute; width:9px; height:40px; background:#999; margin-left:-5px; text-align:center; line-height:40px; color:#666; overflow:hidden; border-left:1px solid #ccc; border-right:1px solid #555;}

------------------

<div id="outer_container">
  <div id="customScrollBox">
    <div class="container">
      <div class="content"> 
      	Nội dung bài viết ở đây
      </div>
    </div>
    <div id="dragger_container">
    <div id="dragger">&#9618;</div>
    </div>
  </div>
</div>

Và toàn bộ mã javascript để xử lý như sau:

$(window).load(function() {
visibleHeight=$('#customScrollBox').height();
if($('#customScrollBox  .container').height()>visibleHeight){
totalContent=$('#customScrollBox  .content').height();
minDraggerHeight=$('#dragger').height();
draggerContainerHeight=$('#dragger_container').height();
adjDraggerHeight=totalContent-((totalContent-visibleHeight)*1.3); //adjust dragger height analogous to content
if(adjDraggerHeight>minDraggerHeight){ //minimum dragger height
	$('#dragger').css('height',adjDraggerHeight+'px');
	$('#dragger').css('line-height',adjDraggerHeight+'px');
} else {
	$('#dragger').css('height',minDraggerHeight+'px');
	$('#dragger').css('line-height',minDraggerHeight+'px');
}
if(adjDraggerHeight<$('#dragger_container').height()){
		$('#dragger').css('top',mouseCoord);
		Scroll();
	} else {
		$('#dragger').css('top',$('#dragger_container').height()-$('#dragger').height());
		Scroll();
	}
});
//mousewheel
$(function($) {
	$('#customScrollBox').bind('mousewheel', function(event, delta) {
		vel = Math.abs(delta*10);
		$('#dragger').css('top', $('#dragger').position().top-(delta*vel));
		Scroll();
		if($('#dragger').position().top<0){
			$('#dragger').css('top', 0);
			$('#customScrollBox .container').stop();
			Scroll();
		}
		if($('#dragger').position().top>$('#dragger_container').height()-$('#dragger').height()){
			$('#dragger').css('top', $('#dragger_container').height()-$('#dragger').height());
			$('#customScrollBox .container').stop();
			Scroll();
		}
		return false;
	});
});
function Scroll(){
	var scrollAmount=(totalContent-(visibleHeight/bottomSpace))/(draggerContainerHeight-draggerHeight);
	var draggerY=$('#dragger').position().top;
	targY=-draggerY*scrollAmount;
	var thePos=$('#customScrollBox  .container').position().top-targY;
	$('#customScrollBox .container').css('top',$('#customScrollBox  .container').position().top-thePos+'px'); //no easing
}
$("#dragger").mouseup(function(){
	DraggerOut();
}).mousedown(function(){
	DraggerOver();
});
function DraggerOver(){
	$('#dragger').css('background-color', '#ccc');
	$('#dragger').css('color', '#666');
	$('#dragger').css('border-left-color', '#fff');
	$('#dragger').css('border-right-color', '#555');
}
function DraggerOut(){
	$('#dragger').css('background-color', '#999');
	$('#dragger').css('color', '#666');
	$('#dragger').css('border-left-color', '#ccc');
	$('#dragger').css('border-right-color', '#555');
}
} else {
	$('#dragger').css('display','none');
	$('#dragger_container').css('display','none');
}
});


Nếu bạn ngại phải đọc & hiểu thì có thể tải về file demo của tác giả và chỉnh sửa lại theo ý của mình.

Demo 1 | Demo 2 | Demo 3 | Download

 
Hiện có 159 khách Trực tuyến

Tư vấn thiết kế web trực tuyến 24/7

thiet ke web, thiet ke website, dich vu thiet ke web
Mr. Thạch
0908 064 643

Dự án thiết kế web mới thực hiện

Dịch vụ thiết kế web trọn gói

Hướng dẫn thiết kế website

Thiet ke web site pro - Tags Cloud


Khách hàng thiết kế web của NQI