This tutorial is for Ext version 2.0.
A complete demonstration is at EasyExt.

EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements ) Is this tutorial for me?

If you are a seasoned programmer then no, I suggest that this is not the page for you, many of the concepts shown will be too basic and as almost all of them are aimed at non application usage (and by this I mean using ExtJs to add interactivity and 'goodness' to a pretty much standard page).

However, if:

  • you want to start with ExtJs at a very basic level or..
  • have very little javascript knowledge - but want some instant results..

This is probably going to be ideal for you!

I have created a website with a growing number of demonstrations and examples with code in them, the link is above on this page.

EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements ) Lets make some sliding text that is activated by a link.

This feature is very common on most websites now, and whilst easy to do - stands as a good example of how basic and easy it can be to do a simple effect using ExtJs.

This tutorial is going to simply identify an id of an element on the page (in this case a div with an id of 'slideme') and by using a built in ExtJs method, slide it up and down underneath another div.

EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements ) The html page.

Nothing too much to fret about here, a simple page with a few divs and some text is about all we need.

EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )          "http://www.w3.org/TR/html4/loose.dtd"
>
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )
<html lang="en">
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )
<head>
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )    
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )    
<title>Sliding Text</title>
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )        
<!-- Bring in the ExtJs Libraries and CSS -->
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )        
<link rel="stylesheet" type="text/css" href="/ext-2.0.2/resources/css/ext-all.css" />
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )        
<script type="text/javascript" src="/ext-2.0.2/adapter/ext/ext-base.js"> </script>
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )        
<script type="text/javascript" src="/ext-2.0.2/ext-all.js"> </script>
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )    
<!-- Place the page specific js here -->
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )        
<script type="text/javascript" src="slidingtext.js"> </script>
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )    
<!-- End page specific js -->
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )        
<!-- Some quick CSS -->
>

EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements ) Start the script.

First we need to make sure that the script doesn't try to access the DOM elements until its all loaded (apart from images) and ready to go. In order to achieve that we use the Ext.onReady() method like this.

//make sure YOUR path is correct to this image!!
Ext.BLANK_IMAGE_URL = 'http://www.cnblogs.com/ext-2.0.2/resources/images/default/s.gif';
 
//this runs on DOM load - you can access all the good stuff now.
 
Ext.onReady(function() {
 
//we put all our code here
});

Note that we included a path to an img file at the top of the script, do this to avoid pulling this file directly from the ExtJs servers - not doing this can slow things down a bit.

EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements ) Now add our Event Handlers

In order to do anything we need to capture the click on our link in the html page, this can be many things - but I would imagine that the standard mouse click is by far and away the one we are interested in!

Luckily ExtJs comes up trumps by cutting down all the guesswork and stress by condensing this into 1 call.

Ext.get('textup').on('click',function(e,t){
	//simple slide of this element
	slideText('up','slider');
	Ext.get(t.id).frame('cccccc',1);
});
 
Ext.get('textdown').on('click',function(e,t){
	//simple slide of this element
	slideText('down','slider');
	Ext.get(t.id).frame('cccccc',1);
});
 
Ext.get('texttoggle').on('click',function(e,t){
	//simple toggle of this element
	slideText('toggle','slider');
	Ext.get(t.id).frame('cccccc',1);
});

This can be broken down in English like this: "When the element 'textup' is clicked upon, call the slideText function with parameters of 'up' and 'slider' and then flash 'textup' using an ExtJs FX method called 'frame'".

You will note that 2 values are passed to the anonymous function from the Ext.get().on handler - these are very useful and represent the 'event' and the 'target' so you can then do things like stopping the event from further detection in your script and most useful you get to know what element was clicked (t).

The 'slide text down' link is very similar except that we change the parameter that we are going to send to the mini function slideText is of course 'down'.

Finally we have the 'texttoggle' event handler which also sends a parameter to the slideText function of 'toggle' - I think a pattern is developing here :)

EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements ) The slideText function.

Now we need a simple function that wraps up the ExtJx FX methods slideIn,slideOut and toggle. By doing this we are being efficient coders and re-using code, we are also going to put some logic in place here by testing the visibility of the element. We don't want to ruin the effect by not checking first to see of the element is hidden or 'up' and vice versa.

We'll use a switch statement to test the inbound parameters as its much tidier than lots of if's and else's!

//simple function to slide text up and down and optionally hide..
var slideText = function(direction,element){
 
var slideMe = Ext.get(element);
 
switch(direction){
	//determine the direction of travel
	case 'up' :
			//lets check to see if this is visible and if not 
                        //then its already hidden.
 
			if (slideMe.isVisible()) {
				//if we get here then the element is visible
				slideMe.slideOut('t', {
					easing: 'easeOut',
					duration: .5,
					remove: false,
					useDisplay: true
				});
			}
		break;
	case 'down' :
			//lets check to see if this is visible and if 
                        //it is then we do nothing.
 
			if (!slideMe.isVisible()) {
				//if we get here then the element is visible
				slideMe.slideIn('t', {
					easing: 'easeOut',
					duration: .5
				});
			}		
		break;
	default :
		//the default action is simply to toggle the element
		slideMe.toggle();
		break
 
}
//ends the slider function	
}

We finally got to the ExtJs methods that do the work here, most of this code is simply wrapping to make the experience for our user a nice one.

EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements ) About the slideIn and slideOut methods.

I won't labour this here, suffice to say that we employ a config option to add parameters to these methods, this is a really easy way to configure ExtJs components.

if (slideMe.isVisible()) {
 
    slideMe.slideOut('t', {
	easing: 'easeOut',
	duration: .5,
	remove: false,
	useDisplay: true
});
 
}

slideMe is an element that we wish to move, we passed this to the function when we called it. We test its visibility (ie: have we already slid it up?) by using the ExtJs method isVisible(), if its visible we then pass into the next section which instructs slideOut() to do its stuff. Note the 't' - this tells slideOut to apply the effect to the top centre edge of the element (vertically upward) - we could have as easily used 'l' and gone left.. its that easy!

Using the config structure we can tell slideOut what we want it to do, here we have chosen to use 'easeOut' easing, to make the whole effect last half a second, not to remove it from the DOM afterwards and finally to use 'display' mode so that we effectively hide it from view - this mode will move the other elements around our target element to fill the place where it was - giving us a most satisfactory animation effect.

EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements ) Summing Up

By now you will have seen that ExtJs does contain a heap of cool stuff which you can start to use and grow your knowledge into something entirely more sophisticated, what we have done here is not complicated, but will give you instant gratification and may encourage you to experiment a little and start to use ExtJs as a tool in your developers kitbag.

EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements ) The Complete Script.

EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )//make sure YOUR path is correct!!
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )
Ext.BLANK_IMAGE_URL = 'http://www.cnblogs.com/ext-2.0.2/resources/images/default/s.gif';
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements ) 
EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )
//this runs on DOM load - you can access all the good stuff now.

EXT基础-元素滑动(Easy Ext - Page Basics -> Sliding Elements )

相关文章: