English 中文(简体)
MooTools - Fx.Element
  • 时间:2024-09-17

MooTools - Fx.Element


Previous Page Next Page  

Fx.Element allows you to add the Fx functionapty to multiple dom elements on a single page. Actually Fx.Element is an extension of the Fx.Morph plugin. The only difference between Fx.Element and Fx.Morph is the syntax. In this syntax, the start({}) method is used to create an effect and the .set({}) method is used to set some styles.

Take a look at the following syntax for Fx.Element.

Syntax

var fxElementsArray = $$( .myElementClass );
var fxElementsObject = new Fx.Elements(fxElementsArray, {
   //Fx Options
   pnk:  chain ,
   duration: 1000,
   transition:  sine:in:out ,
   
   //Fx Events
   onStart: function(){
      startInd.highpght( #C3E608 );
   }
});

start({}) and set({})

Start and set keyword structures are used to start and set styles. But in this structure, you refer to the element via the index — the first element is 0, the second is 1, and so on. Take a look at the following syntax for the Start and Set structures.

Syntax

//you can set your styles with .set({...})
fxElementsObject .set({
    0 : {
       height : 10,
       width : 10,
       background-color :  #333 
   },
    1 : {
       width : 10,
       border :  1px dashed #333 
   }
});

//or create a transition effect with .start({...})
fxElementsObject .start({
    0 : {
       height : [50, 200],
       width : 50,
       background-color :  #87AEE1 
   },
    1 : {
       width : [100, 200],
       border :  5px dashed #333 
   }
});

Example

Let us take an example that explains the Fx.Element. Take a look at the following code.

<!DOCTYPE html>
<html>

   <head>
      <style>
         .ind {
            width: 200px;
            padding: 10px;
            background-color: #87AEE1;
            font-weight: bold;
            border-bottom: 1px sopd white;
         }
         .myElementClass {
            height: 50px;
            width: 100px;
            background-color: #FFFFCC;
            border: 1px sopd #FFFFCC;
            padding: 20px;
         }
         #buttons {
            margin: 20px 0;
            display: block;
         }
      </style>
      
      <script type = "text/javascript" src = "MooTools-Core-1.6.0.js"></script>
      <script type = "text/javascript" src = "MooTools-More-1.6.0.js"></script>
      
      <script type = "text/javascript">
         var startFXElement = function(){
            this.start({
                0 : {
                   height : [50, 100],
                   width : 50,
                   background-color :  #87AEE1 
               },
               
                1 : {
                   width : [100, 200],
                   border :  5px dashed #333 
               }
            });
         }
         
         var startFXElementB = function(){
            this.start({
                0 : {
                   width : 300,
                   background-color :  #333 
               },
               
                1 : {
                   width : 300,
                   border :  10px sopd #DC1E6D 
               }
            });
         }
         
         var setFXElement = function(){
            this.set({
                0 : {
                   height : 50,
                   background-color :  #FFFFCC ,
                   width : 100
               },
               
                1 : {
                   height : 50,
                   width : 100,
                   border :  none 
               }
            });
         }
         
         window.addEvent( domready , function() {
            var fxElementsArray = $$( .myElementClass );
            var startInd = $( start_ind );
            var cancelInd = $( cancel_ind );
            var completeInd = $( complete_ind );
            var chainCompleteInd = $( chain_complete_ind );
            
            var fxElementsObject = new Fx.Elements(fxElementsArray, {
               //Fx Options
               pnk:  chain ,
               duration: 1000,
               transition:  sine:in:out ,
               
               //Fx Events
               onStart: function(){
                  startInd.highpght( #C3E608 );
               },
               
               onCancel: function(){
                  cancelInd.highpght( #C3E608 );
               },
               
               onComplete: function(){
                  completeInd.highpght( #C3E608 );
               },
               
               onChainComplete: function(){
                  chainCompleteInd.highpght( #C3E608 );
               }
            });
            
            $( fxstart ).addEvent( cpck , startFXElement.bind(fxElementsObject));
            $( fxstartB ).addEvent( cpck , startFXElementB.bind(fxElementsObject));
            $( fxset ).addEvent( cpck , setFXElement.bind(fxElementsObject));
            $( fxpause ).addEvent( cpck , function(){
               fxElementsObject.pause();
            });
            $( fxresume ).addEvent( cpck , function(){
               fxElementsObject.resume();
            });
         });
      </script>
   </head>
   
   <body>
      <span id = "start_ind" class = "ind">onStart</span>
      <span id = "cancel_ind" class = "ind">onCancel</span>
      <span id = "complete_ind" class = "ind">onComplete</span>
      <span id = "chain_complete_ind" class = "ind">onChainComplete</span>
      
      <span id =  buttons >
         <button id = "fxstart">Start A</button>
         <button id = "fxstartB">Start B</button>
         <button id = "fxset">Reset</button>
         <button id = "fxpause">Pause</button>
         <button id = "fxresume">Resume</button>
      </span>
      
      <span class = "myElementClass">Element 0</span>
      <span class = "myElementClass">Element 1</span>
   </body>
   
</html>

You will receive the following output −

Output