views:

454

answers:

3

Okay, so I got jQuery to get along with MooTools with one script, by adding this at the top of the jQuery script:

var $j = jQuery.noConflict();

and then replacing every:

$(

with

$j(

But how would you get MooTools to like the following script that using jQuery??

Thanks in advance for any input,

Tracy

//Fade In Content Viewer: By JavaScript Kit: http://www.javascriptkit.com

var fadecontentviewer={
 csszindex: 100,
 fade:function($allcontents, togglerid, selected, speed){
  var selected=parseInt(selected)
  var $togglerdiv=$("#"+togglerid)
  var $target=$allcontents.eq(selected)
  if ($target.length==0){ //if no content exists at this index position (ie: stemming from redundant pagination link)
   alert("No content exists at page number "+selected+"!")
   return 
  }
  if ($togglerdiv.attr('lastselected')==null || parseInt($togglerdiv.attr('lastselected'))!=selected){
   var $toc=$("#"+togglerid+" .toc")
   var $selectedlink=$toc.eq(selected)
   $("#"+togglerid+" .next").attr('nextpage', (selected<$allcontents.length-1)? selected+1+'pg' : 0+'pg')
   $("#"+togglerid+" .prev").attr('previouspage', (selected==0)? $allcontents.length-1+'pg' : selected-1+'pg')
   $target.css({zIndex: this.csszindex++, visibility: 'visible'})
   $target.hide()
   $target.fadeIn(speed)
   $toc.removeClass('selected')
   $selectedlink.addClass('selected')
   $togglerdiv.attr('lastselected', selected+'pg')
  }
 },

 setuptoggler:function($allcontents, togglerid, speed){
  var $toc=$("#"+togglerid+" .toc")
  $toc.each(function(index){
    $(this).attr('pagenumber', index+'pg')
  })

  var $next=$("#"+togglerid+" .next")
  var $prev=$("#"+togglerid+" .prev")
  $next.click(function(){
   fadecontentviewer.fade($allcontents, togglerid, $(this).attr('nextpage'), speed)
   return false
  })
  $prev.click(function(){
   fadecontentviewer.fade($allcontents, togglerid, $(this).attr('previouspage'), speed)
   return false
  })
  $toc.click(function(){
   fadecontentviewer.fade($allcontents, togglerid, $(this).attr('pagenumber'), speed)
   return false
  })
 },

 init:function(fadeid, contentclass, togglerid, selected, speed){
  $(document).ready(function(){
   var faderheight=$("#"+fadeid).height()
   var $fadecontents=$("#"+fadeid+" ."+contentclass)
   $fadecontents.css({top: 0, left: 0, height: faderheight, visibility: 'hidden'})
   fadecontentviewer.setuptoggler($fadecontents, togglerid, speed)
   setTimeout(function(){fadecontentviewer.fade($fadecontents, togglerid, selected, speed)}, 100)
   $(window).bind('unload', function(){ //clean up
    $("#"+togglerid+" .toc").unbind('click')
    $("#"+togglerid+" .next", "#"+togglerid+" .prev").unbind('click')
   })
  })
 }
}
+3  A: 

I don't know about a compatibility mode provided by MooTools, but an easy way should be to replace all occurrences of "$(" in the script by "$j(" (or "jQuery(".

rami
Easy and dirty.
Will Morgan
+4  A: 

When you have jQuery specific code that is using $, the simplest way is to wrap the code with the following:

// Disable the $ global alias completely
jQuery.noConflict();

// For jQuery scripts
(function($){

// set a local $ variable only available in this block as an alias to jQuery
... here is your jQuery specific code ...

})(jQuery);

// For Mootols scripts
(function($){

// set a local $ variable only available in this block as an alias 
// to Mootools document.id
... here is your Mootools specific code ...

})(document.id);

See the second example on noConflict documentation.

Vincent Robert
same thing is true in reverse, i.e. mootools will NOT take over $ if already defined but will revert back to document.id (since v1.2.1). you can then apply the same closure but use `document.id` instead of jQuery as the parameter for $
Dimitar Christoff
Dimitar, you were right on the mark. I had to change all 3 scripts to make 'em work (just changing 1 script didn't work). Adding the var $j = jQuery.noConflict() to the JQuery scripts, to the MooTools script, But not on the 1 above. But I'm glad to know of that shorter tip for when it does work.
flipflopmedia
I included Dimitar solution in my answer to clarify a little bit for Mootools developers
Vincent Robert
A: 

thaks a lot... its working

mohamed Ismail