how to use callback to apply them on newly cloned objects

455 views Asked by At

well i am using quicksand, i want to use tool tips in it, but i am facing problem due to callback code that i am not able to implement, this is the place where quicksand is present and also told about tooltips usage with it, well he has not explained it in detail as expects people to know jquery before using it. http://razorjack.net/quicksand/docs-and-demos.html the code which is saying to use is this

$("#content").quicksand($("#data > li"),
{
duration: 1000,
}, function() { // callback function
$('#content a').tooltip();
}
);

i don't know where to place this code and how as i don't know jquery, and if this code is to be place in tooltips script then where to place in it and how, e-g i might use this one http://www.sohtanaka.com/web-design/...ement-tooltip/ then in this code where to put the above code. If its not possible with the above tooltip then i am ready to use any tooltip which can display picture in it. thanks for reading it and giving me time, please help me as i know for you jquery kings this is not a issue, but is a issue for me who is dumb. lol take care.

2

There are 2 answers

0
Barrie Reader On

You can place jQuery within HTML <script> tags.

e.g.

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
   $(document).ready(function() {
      //jQuery code can go here....

      $("#content").quicksand($("#data > li"), {
        duration: 1000,
      }, function() { // callback function
    $('#content a').tooltip();
  });
   });
</script>
</head>
<body>


</body>
</html>
3
CryOfFaclon On

well thanks for telling me the method but i know that thing, i am just not able to implement in it. Hi first i did this i add this code

$("#gamecategoriest").quicksand(

  $("#data > li"), 
  { duration: 1000 },
  function() { // callback function
        $('#gamecategories a.tip_trigger').hover(function(){
              tip = $(this).find('.tip');
              .....
              tip.css({  top: mousey, left: mousex });
        });
  }

);

just after these lines of code of yours as at the complete end i added the above code $preferences.useScaling = true;

  $performance_container.html($original_html);
  high_performance = true;
}
e.preventDefault();

}); });

so the end result in the end looked like this

<script type="text/javascript"> (function($) { $.fn.sorted = function(customOptions) { var options = { reversed: false, by: function(a) {

return a.text();

} }; $.extend(options, customOptions);

$data = $(this); arr = $data.get(); arr.sort(function(a, b) {

  var valA = options.by($(a));
  var valB = options.by($(b));

if (options.reversed) {

return (valA &lt; valB) ? 1 : (valA &gt; valB) ? -1 : 0;    

} else {

return (valA &lt; valB) ? -1 : (valA &gt; valB) ? 1 : 0; 

} }); return $(arr); };

})(jQuery);

$(function() {

var read_button = function(class_names) {

var r = {
  selected: false,
  type: 0
};
for (var i=0; i &lt; class_names.length; i++) {
  if (class_names[i].indexOf(&#39;selected-&#39;) == 0) {
    r.selected = true;
  }
  if (class_names[i].indexOf(&#39;segment-&#39;) == 0) {
    r.segment = class_names[i].split(&#39;-&#39;)[1];
  }
};
return r;

};

var determine_sort = function($buttons) {

var $selected = $buttons.parent().filter(&#39;[class*=&quot;selected-&quot;]&#39;);
return $selected.find(&#39;a&#39;).attr(&#39;data-value&#39;);

};

var determine_kind = function($buttons) {

var $selected = $buttons.parent().filter(&#39;[class*=&quot;selected-&quot;]&#39;);
return $selected.find(&#39;a&#39;).attr(&#39;data-value&#39;);

};

var $preferences = {

duration: 800,
easing: &#39;easeInOutQuad&#39;,
adjustHeight: false

};

var $list = $('#data'); var $data = $list.clone();

var $controls = $('ul#gamecategories ul');

$controls.each(function(i) {

var $control = $(this);
var $buttons = $control.find(&#39;a&#39;);

$buttons.bind(&#39;click&#39;, function(e) {

  var $button = $(this);
  var $button_container = $button.parent();
  var button_properties = read_button($button_container.attr(&#39;class&#39;).split(&#39; &#39;));      
  var selected = button_properties.selected;
  var button_segment = button_properties.segment;

  if (!selected) {

    $buttons.parent().removeClass(&#39;selected-0&#39;).removeClass(&#39;selected-1&#39;).removeClass(&#39;selected-2&#39;);
    $button_container.addClass(&#39;selected-&#39; + button_segment);

    var sorting_type = determine_sort($controls.eq(1).find(&#39;a&#39;));
    var sorting_kind = determine_kind($controls.eq(0).find(&#39;a&#39;));

    if (sorting_kind == &#39;all&#39;) {
      var $filtered_data = $data.find(&#39;li&#39;);
    } else {
      var $filtered_data = $data.find(&#39;li.&#39; + sorting_kind);
    }

    if (sorting_type == &#39;size&#39;) {
      var $sorted_data = $filtered_data.sorted({
        by: function(v) {
          return parseFloat($(v).find(&#39;span&#39;).text());
        }
      });
    } else {
      var $sorted_data = $filtered_data.sorted({
        by: function(v) {
          return $(v).find(&#39;strong&#39;).text().toLowerCase();
        }
      });
    }

    $list.quicksand($sorted_data, $preferences);

  }

  e.preventDefault();
});

});

var high_performance = true;
var $performance_container = $('#performance-toggle'); var $original_html = $performance_container.html();

$performance_container.find('a').live('click', function(e) {

if (high_performance) {
  $preferences.useScaling = false;
  $performance_container.html(&#39;CSS3 scaling turned off. Try the demo again. &lt;a href=&quot;#toggle&quot;&gt;Reverse&lt;/a&gt;.&#39;);
  high_performance = false;
} else {
  $preferences.useScaling = true;
  $performance_container.html($original_html);
  high_performance = true;
}
e.preventDefault();

}); });

$("#gamecategoriest").quicksand(

  $("#data > li"), 
  { duration: 1000 },
  function() { // callback function
        $('#gamecategories a.tip_trigger').hover(function(){
              tip = $(this).find('.tip');
              .....
              tip.css({  top: mousey, left: mousex });
        });
  }

); </script>

by doing above thing my tooltip was working but after animation it stopped. So i did this then i replaced this $list.quicksand($sorted_data, $preferences); with this $list.quicksand($sorted_data, $preferences, function () { $(this).tooltip (); } ); and i added this code

jQuery.fn.tooltip = function () {

this.each ( function ( index, element ) {
    $(element).mouseover(function(){
        tip = $(this).find('.tip');
         tip.show(); //Show tooltip
     }).mouseout ( function() {
         tip.hide(); //Hide tooltip
     }).mousemove(function(e) {
         var mousex = e.pageX + 20; //Get X coodrinates
         var mousey = e.pageY + 20; //Get Y coordinates
         var tipWidth = tip.width(); //Find width of tooltip
         var tipHeight = tip.height(); //Find height of tooltip

         //Distance of element from the right edge of viewport
         var tipVisX = $(window).width() - (mousex + tipWidth);
         //Distance of element from the bottom of viewport
         var tipVisY = $(window).height() - (mousey + tipHeight);

         if ( tipVisX < 20 ) { //If tooltip exceeds the X coordinate of viewport
             mousex = e.pageX - tipWidth - 20;
         } if ( tipVisY < 20 ) { //If tooltip exceeds the Y coordinate of viewport
             mousey = e.pageY - tipHeight - 20;
         }
         //Absolute position the tooltip according to mouse position
         tip.css({  top: mousey, left: mousex });
     });
});

};

just after this code at the end of your code

$preferences.useScaling = true;

  $performance_container.html($original_html);
  high_performance = true;
}
e.preventDefault();

}); });

so the end result will look like this

<script type="text/javascript"> (function($) { $.fn.sorted = function(customOptions) { var options = { reversed: false, by: function(a) {

return a.text();

} }; $.extend(options, customOptions);

$data = $(this); arr = $data.get(); arr.sort(function(a, b) {

  var valA = options.by($(a));
  var valB = options.by($(b));

if (options.reversed) {

return (valA &lt; valB) ? 1 : (valA &gt; valB) ? -1 : 0;    

} else {

return (valA &lt; valB) ? -1 : (valA &gt; valB) ? 1 : 0; 

} }); return $(arr); };

})(jQuery);

$(function() {

var read_button = function(class_names) {

var r = {
  selected: false,
  type: 0
};
for (var i=0; i &lt; class_names.length; i++) {
  if (class_names[i].indexOf(&#39;selected-&#39;) == 0) {
    r.selected = true;
  }
  if (class_names[i].indexOf(&#39;segment-&#39;) == 0) {
    r.segment = class_names[i].split(&#39;-&#39;)[1];
  }
};
return r;

};

var determine_sort = function($buttons) {

var $selected = $buttons.parent().filter(&#39;[class*=&quot;selected-&quot;]&#39;);
return $selected.find(&#39;a&#39;).attr(&#39;data-value&#39;);

};

var determine_kind = function($buttons) {

var $selected = $buttons.parent().filter(&#39;[class*=&quot;selected-&quot;]&#39;);
return $selected.find(&#39;a&#39;).attr(&#39;data-value&#39;);

};

var $preferences = {

duration: 800,
easing: &#39;easeInOutQuad&#39;,
adjustHeight: false

};

var $list = $('#data'); var $data = $list.clone();

var $controls = $('ul#gamecategories ul');

$controls.each(function(i) {

var $control = $(this);
var $buttons = $control.find(&#39;a&#39;);

$buttons.bind(&#39;click&#39;, function(e) {

  var $button = $(this);
  var $button_container = $button.parent();
  var button_properties = read_button($button_container.attr(&#39;class&#39;).split(&#39; &#39;));      
  var selected = button_properties.selected;
  var button_segment = button_properties.segment;

  if (!selected) {

    $buttons.parent().removeClass(&#39;selected-0&#39;).removeClass(&#39;selected-1&#39;).removeClass(&#39;selected-2&#39;);
    $button_container.addClass(&#39;selected-&#39; + button_segment);

    var sorting_type = determine_sort($controls.eq(1).find(&#39;a&#39;));
    var sorting_kind = determine_kind($controls.eq(0).find(&#39;a&#39;));

    if (sorting_kind == &#39;all&#39;) {
      var $filtered_data = $data.find(&#39;li&#39;);
    } else {
      var $filtered_data = $data.find(&#39;li.&#39; + sorting_kind);
    }

    if (sorting_type == &#39;size&#39;) {
      var $sorted_data = $filtered_data.sorted({
        by: function(v) {
          return parseFloat($(v).find(&#39;span&#39;).text());
        }
      });
    } else {
      var $sorted_data = $filtered_data.sorted({
        by: function(v) {
          return $(v).find(&#39;strong&#39;).text().toLowerCase();
        }
      });
    }

    $list.quicksand($sorted_data, $preferences, function () { $(this).tooltip (); } );

  }

  e.preventDefault();
});

});

var high_performance = true;
var $performance_container = $('#performance-toggle'); var $original_html = $performance_container.html();

$performance_container.find('a').live('click', function(e) {

if (high_performance) {
  $preferences.useScaling = false;
  $performance_container.html(&#39;CSS3 scaling turned off. Try the demo again. &lt;a href=&quot;#toggle&quot;&gt;Reverse&lt;/a&gt;.&#39;);
  high_performance = false;
} else {
  $preferences.useScaling = true;
  $performance_container.html($original_html);
  high_performance = true;
}
e.preventDefault();

}); });

jQuery.fn.tooltip = function () {

this.each ( function ( index, element ) {
    $(element).mouseover(function(){
        tip = $(this).find(&#39;.tip&#39;);
         tip.show(); //Show tooltip
     }).mouseout ( function() {
         tip.hide(); //Hide tooltip
     }).mousemove(function(e) {
         var mousex = e.pageX + 20; //Get X coodrinates
         var mousey = e.pageY + 20; //Get Y coordinates
         var tipWidth = tip.width(); //Find width of tooltip
         var tipHeight = tip.height(); //Find height of tooltip

         //Distance of element from the right edge of viewport
         var tipVisX = $(window).width() - (mousex + tipWidth);
         //Distance of element from the bottom of viewport
         var tipVisY = $(window).height() - (mousey + tipHeight);

         if ( tipVisX &lt; 20 ) { //If tooltip exceeds the X coordinate of viewport
             mousex = e.pageX - tipWidth - 20;
         } if ( tipVisY &lt; 20 ) { //If tooltip exceeds the Y coordinate of viewport
             mousey = e.pageY - tipHeight - 20;
         }
         //Absolute position the tooltip according to mouse position
         tip.css({  top: mousey, left: mousex });
     });
});

}; </script>

i think i am adding the codes to the wrong placesin your given script the test page is at http://letseedrop.blogspot.com/2011/01/testing-3.html and if the code is not very well readable then you can check it from here too i posted a comment there too https://github.com/razorjack/quicksand/issues/44