Get the First Touch on JQuery

Objective:


Actually, what I am going to do is integrate this JQuery UI into my Joomla! site: http://jqueryui.com/demos/slider/#side-scroll. But I have no idea about JQuery, my learning progress now is just going onto Ajax. 


JQuery's Official Tutorial:


So this blog will be my note on walking through the quick-start tutorial from JQuery's official site.


After reading this article: How jQuery Works?, I still don't know how it works. And then take a look at <Getting Started with jQuery>, with the following samples:


The first step is download jQuery.js file: http://docs.jquery.com/Downloading_jQuery

There are only two variants available for almost all of the versions of jQuery, and according to the instruction before the links, the min version should work best among all. Then download jquery-1.7.1.min.js, as I should have no time to investigate the jQuery source code.


I would take some space for the second sample, which sets the HTML element's style with jQuery, and its code should be like:


<html>                                                                  
	<head>   
		<style>
			#orderedlist.red { background: red; }
		</style>
		<script type="text/javascript" src="current-rel/jquery-1.7.1.min.js"></script>          
		<script type="text/javascript">                                         
			$(document).ready(
				function() {$("#orderedlist").addClass("red");}
			); 
		</script>                                                               
	</head>                                                                 
<body>                                                                  
	<ol id="orderedlist">
		<li>Enric</li>
		<li>Tim</li>
		<li>Bob</li>
		<li>Tina</li>
	</ol>                                       
</body>                                                                 
</html>

The style block is referenced to the article: How jQuery Works?. But very soon I found that the selector #orderedlist was not a must. This sample could work without it:


<html>                                                                  
	<head>   
		<style>
			.red { background: red; }
		</style>
		<script type="text/javascript" src="current-rel/jquery-1.7.1.min.js"></script>          
		<script type="text/javascript">                                         
			$(document).ready(
				function() {$("#orderedlist").addClass("red");}
			); 
		</script>                                                               
	</head>                                                                 
<body>                                                                  
	<ol id="orderedlist">
		<li>Enric</li>
		<li>Tim</li>
		<li>Bob</li>
		<li>Tina</li>
	</ol>                                       
</body>                                                                 
</html>

Finally, I got the sample program run as:


hover-sample


when the mouse is over the last item, its background color would change to green. 


Something clear now:


1. $ is an alias for the jQuery "class", therefore $() constructs and return a new jQuery object, and click() is its method, what it does is binding the offered functions to all the selected elements' click events, addClass() is also its method as well. About other events you can bind, refer to Events.

2. The string value passed to $() is selector, about selector, refer to Selectors, and API/1.1.2/DOM/Traversing/Selectors. jQuery support powerful utilities to traverse through the DOM, you can get your elements by very complex rule.

3. $(document).ready() is always where you can place your initial logic immediately after the page got loaded.


But something that I am really confused:


1. The source-code provided in the jQuery UI demo page of slider-bar does not reflect the above rules, there is no call to$(document).ready(), instead, the script starts with$(function(){...}), what is it?

2. On the right bottom of the dome page, it states that the dependence of this UI widget includes:UI Core,UI Widget andUI Mouse, what are they and where can I get them?


The worse is that the source-code of the demo provided totally differs from the source code I got from Chrome inspector :


<meta charset="utf-8">
	
	<style>
	#demo-frame > div.demo { padding: 10px !important; }
	.scroll-pane { overflow: auto; width: 99%; float:left; }
	.scroll-content { width: 2440px; float: left; }
	.scroll-content-item { width: 100px; height: 100px; float: left; margin: 10px; font-size: 3em; line-height: 96px; text-align: center; }
	* html .scroll-content-item { display: inline; } /* IE6 float double margin bug */
	.scroll-bar-wrap { clear: left; padding: 0 4px 0 2px; margin: 0 -1px -1px -1px; }
	.scroll-bar-wrap .ui-slider { background: none; border:0; height: 2em; margin: 0 auto;  }
	.scroll-bar-wrap .ui-handle-helper-parent { position: relative; width: 100%; height: 100%; margin: 0 auto; }
	.scroll-bar-wrap .ui-slider-handle { top:.2em; height: 1.5em; }
	.scroll-bar-wrap .ui-slider-handle .ui-icon { margin: -8px auto 0; position: relative; top: 50%; }
	</style>
	<script>
	$(function() {
		//scrollpane parts
		var scrollPane = $( ".scroll-pane" ),
			scrollContent = $( ".scroll-content" );
		
		//build slider
		var scrollbar = $( ".scroll-bar" ).slider({
			slide: function( event, ui ) {
				if ( scrollContent.width() > scrollPane.width() ) {
					scrollContent.css( "margin-left", Math.round(
						ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
					) + "px" );
				} else {
					scrollContent.css( "margin-left", 0 );
				}
			}
		});
		
		//append icon to handle
		var handleHelper = scrollbar.find( ".ui-slider-handle" )
		.mousedown(function() {
			scrollbar.width( handleHelper.width() );
		})
		.mouseup(function() {
			scrollbar.width( "100%" );
		})
		.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
		.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
		
		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css( "overflow", "hidden" );
		
		//size scrollbar and handle proportionally to scroll distance
		function sizeScrollbar() {
			var remainder = scrollContent.width() - scrollPane.width();
			var proportion = remainder / scrollContent.width();
			var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
			scrollbar.find( ".ui-slider-handle" ).css({
				width: handleSize,
				"margin-left": -handleSize / 2
			});
			handleHelper.width( "" ).width( scrollbar.width() - handleSize );
		}
		
		//reset slider value based on scroll content position
		function resetValue() {
			var remainder = scrollPane.width() - scrollContent.width();
			var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
				parseInt( scrollContent.css( "margin-left" ) );
			var percentage = Math.round( leftVal / remainder * 100 );
			scrollbar.slider( "value", percentage );
		}
		
		//if the slider is 100% and window gets larger, reveal content
		function reflowContent() {
				var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
				var gap = scrollPane.width() - showing;
				if ( gap > 0 ) {
					scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
				}
		}
		
		//change handle position on window resize
		$( window ).resize(function() {
			resetValue();
			sizeScrollbar();
			reflowContent();
		});
		//init scrollbar size
		setTimeout( sizeScrollbar, 10 );//safari wants a timeout
	});
	</script>

<div class="demo">

<div class="scroll-pane ui-widget ui-widget-header ui-corner-all">
	<div class="scroll-content">
		<div class="scroll-content-item ui-widget-header">1</div>
		<div class="scroll-content-item ui-widget-header">2</div>
		<div class="scroll-content-item ui-widget-header">3</div>
		<div class="scroll-content-item ui-widget-header">4</div>
		<div class="scroll-content-item ui-widget-header">5</div>
		<div class="scroll-content-item ui-widget-header">6</div>
		<div class="scroll-content-item ui-widget-header">7</div>
		<div class="scroll-content-item ui-widget-header">8</div>
		<div class="scroll-content-item ui-widget-header">9</div>
		<div class="scroll-content-item ui-widget-header">10</div>
		<div class="scroll-content-item ui-widget-header">11</div>
		<div class="scroll-content-item ui-widget-header">12</div>
		<div class="scroll-content-item ui-widget-header">13</div>
		<div class="scroll-content-item ui-widget-header">14</div>
		<div class="scroll-content-item ui-widget-header">15</div>
		<div class="scroll-content-item ui-widget-header">16</div>
		<div class="scroll-content-item ui-widget-header">17</div>
		<div class="scroll-content-item ui-widget-header">18</div>
		<div class="scroll-content-item ui-widget-header">19</div>
		<div class="scroll-content-item ui-widget-header">20</div>
	</div>
	<div class="scroll-bar-wrap ui-widget-content ui-corner-bottom">
		<div class="scroll-bar"></div>
	</div>
</div>

</div><!-- End demo -->



<div class="demo-description">
<p>Use a slider to manipulate the positioning of content on the page. In this case, it acts as a scrollbar with the potential to capture values if needed.</p>
</div><!-- End demo-description -->

 
<!DOCTYPE html>
<html>
<head>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
    <style type="text/css">
    #slider { margin: 10px; }
  </style>
  <script>
  $(document).ready(function() {
    $("#slider").slider();
  });
  </script>
</head>
<body style="font-size:62.5%;">
  
<div id="slider"></div>

</body>
</html>

If I save the latter HTML code and open it through web server, I got result:


slider


It couldn't be what we are expecting. And I found that we can open the demo in a new window for easy inspection:


demo-new-win





The source code of the new page is(by Chrome): 


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>jQuery UI Slider - Slider scrollbar</title>
	<link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
	<script src="../../jquery-1.7.1.js"></script>
	<script src="../../ui/jquery.ui.core.js"></script>
	<script src="../../ui/jquery.ui.widget.js"></script>
	<script src="../../ui/jquery.ui.mouse.js"></script>
	<script src="../../ui/jquery.ui.slider.js"></script>
	<link rel="stylesheet" href="../demos.css">
	<style>
	#demo-frame > div.demo { padding: 10px !important; }
	.scroll-pane { overflow: auto; width: 99%; float:left; }
	.scroll-content { width: 2440px; float: left; }
	.scroll-content-item { width: 100px; height: 100px; float: left; margin: 10px; font-size: 3em; line-height: 96px; text-align: center; }
	* html .scroll-content-item { display: inline; } /* IE6 float double margin bug */
	.scroll-bar-wrap { clear: left; padding: 0 4px 0 2px; margin: 0 -1px -1px -1px; }
	.scroll-bar-wrap .ui-slider { background: none; border:0; height: 2em; margin: 0 auto;  }
	.scroll-bar-wrap .ui-handle-helper-parent { position: relative; width: 100%; height: 100%; margin: 0 auto; }
	.scroll-bar-wrap .ui-slider-handle { top:.2em; height: 1.5em; }
	.scroll-bar-wrap .ui-slider-handle .ui-icon { margin: -8px auto 0; position: relative; top: 50%; }
	</style>
	<script>
	$(function() {
		//scrollpane parts
		var scrollPane = $( ".scroll-pane" ),
			scrollContent = $( ".scroll-content" );
		
		//build slider
		var scrollbar = $( ".scroll-bar" ).slider({
			slide: function( event, ui ) {
				if ( scrollContent.width() > scrollPane.width() ) {
					scrollContent.css( "margin-left", Math.round(
						ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
					) + "px" );
				} else {
					scrollContent.css( "margin-left", 0 );
				}
			}
		});
		
		//append icon to handle
		var handleHelper = scrollbar.find( ".ui-slider-handle" )
		.mousedown(function() {
			scrollbar.width( handleHelper.width() );
		})
		.mouseup(function() {
			scrollbar.width( "100%" );
		})
		.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
		.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
		
		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css( "overflow", "hidden" );
		
		//size scrollbar and handle proportionally to scroll distance
		function sizeScrollbar() {
			var remainder = scrollContent.width() - scrollPane.width();
			var proportion = remainder / scrollContent.width();
			var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
			scrollbar.find( ".ui-slider-handle" ).css({
				width: handleSize,
				"margin-left": -handleSize / 2
			});
			handleHelper.width( "" ).width( scrollbar.width() - handleSize );
		}
		
		//reset slider value based on scroll content position
		function resetValue() {
			var remainder = scrollPane.width() - scrollContent.width();
			var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
				parseInt( scrollContent.css( "margin-left" ) );
			var percentage = Math.round( leftVal / remainder * 100 );
			scrollbar.slider( "value", percentage );
		}
		
		//if the slider is 100% and window gets larger, reveal content
		function reflowContent() {
				var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
				var gap = scrollPane.width() - showing;
				if ( gap > 0 ) {
					scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
				}
		}
		
		//change handle position on window resize
		$( window ).resize(function() {
			resetValue();
			sizeScrollbar();
			reflowContent();
		});
		//init scrollbar size
		setTimeout( sizeScrollbar, 10 );//safari wants a timeout
	});
	</script>
</head>
<body>

<div class="demo">

<div class="scroll-pane ui-widget ui-widget-header ui-corner-all">
	<div class="scroll-content">
		<div class="scroll-content-item ui-widget-header">1</div>
		<div class="scroll-content-item ui-widget-header">2</div>
		<div class="scroll-content-item ui-widget-header">3</div>
		<div class="scroll-content-item ui-widget-header">4</div>
		<div class="scroll-content-item ui-widget-header">5</div>
		<div class="scroll-content-item ui-widget-header">6</div>
		<div class="scroll-content-item ui-widget-header">7</div>
		<div class="scroll-content-item ui-widget-header">8</div>
		<div class="scroll-content-item ui-widget-header">9</div>
		<div class="scroll-content-item ui-widget-header">10</div>
		<div class="scroll-content-item ui-widget-header">11</div>
		<div class="scroll-content-item ui-widget-header">12</div>
		<div class="scroll-content-item ui-widget-header">13</div>
		<div class="scroll-content-item ui-widget-header">14</div>
		<div class="scroll-content-item ui-widget-header">15</div>
		<div class="scroll-content-item ui-widget-header">16</div>
		<div class="scroll-content-item ui-widget-header">17</div>
		<div class="scroll-content-item ui-widget-header">18</div>
		<div class="scroll-content-item ui-widget-header">19</div>
		<div class="scroll-content-item ui-widget-header">20</div>
	</div>
	<div class="scroll-bar-wrap ui-widget-content ui-corner-bottom">
		<div class="scroll-bar"></div>
	</div>
</div>

</div><!-- End demo -->



<div class="demo-description">
<p>Use a slider to manipulate the positioning of content on the page. In this case, it acts as a scrollbar with the potential to capture values if needed.</p>
</div><!-- End demo-description -->

</body>
</html>

It loads four js file in header:


jquery.ui.core.js
jquery.ui.widget.js
jquery.ui.mouse.js
jquery.ui.slider.js


In contrast, the demo page loads:


http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.17/jquery-ui.min.js
http://jquery-ui.googlecode.com/svn/tags/latest/external/jquery.bgiframe-2.1.2.js
http://jquery-ui.googlecode.com/svn/tags/latest/ui/minified/i18n/jquery-ui-i18n.min.js


Of course, apart of jQuery.js. It seems that the jQuery UI has its own specific structure.


JQuery UI Official Tutorial:


Just navigate here from the home page: Getting Started. And it is so simple to build a customized jQuery-UI library to download. So I deselected all the components except: slider, and select 'South Street' theme.


The file inside the zip archive: 


file-structure 


Indeed, the folder 'development-bundle' is not necessary for my case; as you see, I created a HTML file: scrollbar.html; its content is the source code from the scroll-bar demo:http://jqueryui.com/demos/slider/side-scroll.html


Of course, it needs customization in terms of the referencing CSS and JS files:


<meta charset="utf-8">
<title>jQuery UI Slider - Slider scrollbar</title>
<link rel="stylesheet" href="css/south-street/jquery-ui-1.8.17.custom.css">
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery-ui-1.8.17.custom.min.js"></script>

Keep others unchanged for now, and open the file via the server, it works!!!


Now, I replace the squares with a very wide table, and remove the width specification for scroll-content in style block, my final code:


<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<title>jQuery UI Slider - Slider scrollbar</title>
	<link rel="stylesheet" href="css/south-street/jquery-ui-1.8.17.custom.css">
	<script src="js/jquery-1.7.1.min.js"></script>
	<script src="js/jquery-ui-1.8.17.custom.min.js"></script>

	<style>
	#demo-frame > div.demo { padding: 10px !important; }
	.scroll-pane { overflow: auto; width: 99%; float:left; }
	.scroll-content { float: left; }
	.scroll-content-item { width: 100px; height: 100px; float: left; margin: 10px; font-size: 3em; line-height: 96px; text-align: center; }
	* html .scroll-content-item { display: inline; } /* IE6 float double margin bug */
	.scroll-bar-wrap { clear: left; padding: 0 4px 0 2px; margin: 0 -1px -1px -1px; }
	.scroll-bar-wrap .ui-slider { background: none; border:0; height: 2em; margin: 0 auto;  }
	.scroll-bar-wrap .ui-handle-helper-parent { position: relative; width: 100%; height: 100%; margin: 0 auto; }
	.scroll-bar-wrap .ui-slider-handle { top:.2em; height: 1.5em; }
	.scroll-bar-wrap .ui-slider-handle .ui-icon { margin: -8px auto 0; position: relative; top: 50%; }
	</style>
	<script>
	$(function() {
		//scrollpane parts
		var scrollPane = $( ".scroll-pane" ),
			scrollContent = $( ".scroll-content" );
		
		//build slider
		var scrollbar = $( ".scroll-bar" ).slider({
			slide: function( event, ui ) {
				if ( scrollContent.width() > scrollPane.width() ) {
					scrollContent.css( "margin-left", Math.round(
						ui.value / 100 * ( scrollPane.width() - scrollContent.width() )
					) + "px" );
				} else {
					scrollContent.css( "margin-left", 0 );
				}
			}
		});
		
		//append icon to handle
		var handleHelper = scrollbar.find( ".ui-slider-handle" )
		.mousedown(function() {
			scrollbar.width( handleHelper.width() );
		})
		.mouseup(function() {
			scrollbar.width( "100%" );
		})
		.append( "<span class='ui-icon ui-icon-grip-dotted-vertical'></span>" )
		.wrap( "<div class='ui-handle-helper-parent'></div>" ).parent();
		
		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css( "overflow", "hidden" );
		
		//size scrollbar and handle proportionally to scroll distance
		function sizeScrollbar() {
			var remainder = scrollContent.width() - scrollPane.width();
			var proportion = remainder / scrollContent.width();
			var handleSize = scrollPane.width() - ( proportion * scrollPane.width() );
			scrollbar.find( ".ui-slider-handle" ).css({
				width: handleSize,
				"margin-left": -handleSize / 2
			});
			handleHelper.width( "" ).width( scrollbar.width() - handleSize );
		}
		
		//reset slider value based on scroll content position
		function resetValue() {
			var remainder = scrollPane.width() - scrollContent.width();
			var leftVal = scrollContent.css( "margin-left" ) === "auto" ? 0 :
				parseInt( scrollContent.css( "margin-left" ) );
			var percentage = Math.round( leftVal / remainder * 100 );
			scrollbar.slider( "value", percentage );
		}
		
		//if the slider is 100% and window gets larger, reveal content
		function reflowContent() {
				var showing = scrollContent.width() + parseInt( scrollContent.css( "margin-left" ), 10 );
				var gap = scrollPane.width() - showing;
				if ( gap > 0 ) {
					scrollContent.css( "margin-left", parseInt( scrollContent.css( "margin-left" ), 10 ) + gap );
				}
		}
		
		//change handle position on window resize
		$( window ).resize(function() {
			resetValue();
			sizeScrollbar();
			reflowContent();
		});
		//init scrollbar size
		setTimeout( sizeScrollbar, 10 );//safari wants a timeout
	});
	</script>
</head>
<body>

<div class="demo">

<div class="scroll-pane ui-widget ui-widget-header ui-corner-all">
	<div class="scroll-content">
		<table style="width:3900px; border: 1px solid black">
		<tr><td>hello!</td></tr>
		</table>
	</div>
	<div class="scroll-bar-wrap ui-widget-content ui-corner-bottom">
		<div class="scroll-bar"></div>
	</div>
</div>

</div><!-- End demo -->



<div class="demo-description">
<p>Use a slider to manipulate the positioning of content on the page. In this case, it acts as a scrollbar with the potential to capture values if needed.</p>
</div><!-- End demo-description -->

</body>
</html>

my-scroll-bar-sample


So far so good, that is how to get the jQuery UI code work for me. Now, verify that would work in other browsers:


scroll-bar-in-FF

 

scroll-bar-in-IE


When you resize the window, the program will redraw the bar to present it properly. What a wonderful tool!!!


But my trial is not finished yet, paste the style and script blocks into body element, inside the tag '<div class="demo">', and test it in the three browsers, it still works fine! I did that because I need to plug this UI into my Joomla! site, and I might not be able to put them inside header all the time.



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值