Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Maybe this could be optimized somehow and yet I have tested it in Chrome only, but I think it will work in other browsers too. There is no need of jQuery UI for this, it's hand made ;)</p> <pre><code>$(function() { var selectableLi = $('#selectable li'); selectableLi.mousedown(function(){ var startIndex, endIndex, mouseUpOnLi = false; // When dragging starts, remove classes active and hover selectableLi.removeClass('active hover'); // Give the element where dragging starts a class $(this).addClass('active'); // Save the start index startIndex = $(this).index(); // Bind mouse up event selectableLi.bind('mouseup', function(){ // Mouse up is on a li-element mouseUpOnLi = true; $(this).addClass('active'); // Remove the events for mouseup, mouseover and mouseout selectableLi.unbind('mouseup mouseover mouseout'); // Store the end index endIndex = $(this).index(); // Swap values if endIndex &lt; startindex if(endIndex &lt; startIndex){ var tmp = startIndex; startIndex = endIndex; endIndex = tmp; } // Give the selected elements a colour for(i=startIndex; i&lt;=endIndex; i++){ $(selectableLi[i]).addClass('active'); } }).bind('mouseover', function(){ // Give elements a hover class when hovering $(this).addClass('hover'); }).bind('mouseout', function(){ // Remove the hover class when mouse moves out the li $(this).removeClass('hover'); }); $(document).bind('mouseup', function(e){ // When mouse up is outside a li-element if(!mouseUpOnLi){ selectableLi.removeClass('active'); } $(this).unbind('mouseup'); }); }).attr("unselectable","on").css("MozUserSelect","none").bind("selectstart",function(){return false}); }); </code></pre> <p>I've got an <strong><a href="http://harmen.no-ip.org/so/selection.html" rel="nofollow noreferrer">example online</a></strong>. Note that items don't have a background colour when selecting; I think this will give a better performance.</p> <hr> <p><strong>UPDATE - <a href="http://harmen.no-ip.org/so/selection2.html" rel="nofollow noreferrer">Example 2</a></strong></p> <p>I updated it so the selection is visible while selecting:</p> <pre><code>var selectableLi; function colourSelected(a, b, Class){ selectableLi.removeClass(Class); // Swap values if a &gt; b if(a &gt; b){ var tmp = a; a = b; b = tmp; } // Give the selected elements a colour for(i=a; i&lt;=b; i++){ $(selectableLi[i]).addClass(Class); } } $(function() { selectableLi = $('#selectable li'); selectableLi.mousedown(function(){ var startIndex, endIndex, mouseUpOnLi = false; // When dragging starts, remove classes active and hover selectableLi.removeClass('active hover'); // Give the element where dragging starts a class $(this).addClass('active'); // Save the start index startIndex = $(this).index(); // Bind mouse up event selectableLi.bind('mouseup', function(){ // Mouse up is on a li-element mouseUpOnLi = true; $(this).addClass('active'); // Remove the events for mouseup, mouseover and mouseout selectableLi.unbind('mouseup mouseover mouseout'); colourSelected(startIndex, $(this).index(), 'active'); }).bind('mouseover mouseout', function(){ // Give elements a hover class when hovering colourSelected(startIndex, $(this).index(), 'hover'); }); $(document).bind('mouseup', function(e){ // When mouse up is outside a li-element if(!mouseUpOnLi){ selectableLi.removeClass('active hover'); } $(this).unbind('mouseup'); selectableLi.unbind('mouseover mouseout'); }); }).attr("unselectable","on").css("MozUserSelect","none").bind("selectstart",function(){return false}); }); </code></pre> <p>Again, maybe this code could be optimized somehow for performance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload