2014-09-05 111 views
0

我有一個Joomla網站,其中包含一個基於同位素和touchtouch的投資組合的模板 我有與另一篇文章中描述的相同的問題 - 唯一的區別是,他們使用fancybox和我的模板使用touchtouch:jQuery Isotope filtering with FancyboxjQuery同位素過濾

這裏的問題的描述:

產品組合中包含縮略圖的畫廊。 您可以點擊縮略圖,然後用觸摸觸摸插件 循環顯示圖像。現在,當圖庫被過濾時,觸摸觸摸插件仍會循環顯示所有圖像。即使是應用過濾器時也沒有出現的人。

我試圖改變fancybox-post的解決方案,但仍然不知如何解決這個問題。

我想,應該修改下面的腳本gallery.php:

<script type="text/javascript"> 
    jQuery(document).ready(function() { 
    (function($){ 
    $(window).load(function(){ 

    var $container = $('#isotopeContainer'); 

    // filter items when filter link is clicked 
    $('#filters a').click(function(){ 
     var selector = $(this).attr('data-filter'); 
     $container.isotope({ filter: selector }); 
     return false; 
    }); 

    var $optionSets = $('#filters li'), 
     $optionLinks = $optionSets.find('a'); 

     $optionLinks.click(function(){ 
      var $this = $(this); 
      // don't proceed if already selected 
      if ($this.hasClass('selected')) { 
       return false; 
      } 
      var $optionSet = $this.parents('#filters'); 
      $optionSet.find('.selected').removeClass('selected'); 
      $this.addClass('selected'); 

      // make option object dynamically, i.e. { filter: '.my-filter-class' } 
      var options = {}, 
       key = $optionSet.attr('data-option-key'), 
       value = $this.attr('data-option-value'); 
      // parse 'false' as false boolean 
      value = value === 'false' ? false : value; 
      options[ key ] = value; 
      if (key === 'layoutMode' && typeof changeLayoutMode === 'function') { 
       // changes in layout modes need extra logic 
       changeLayoutMode($this, options) 
      } else { 
       // otherwise, apply new options 
       $container.isotope(options); 
      } 

      return false; 
     }); 
    }); 
})(jQuery); 
}); 
</script> 

你有任何想法如何做到這一點?

回答

0

我很高興地說,我已經做到了 我的第一個想法(見前面的帖子)不適合我,但我現在更好地瞭解它是如何工作的。 所以我找到了另一種方法來做到這一點:現在我展示了元素上的類別和

var overlay = $('<div id="galleryOverlay">'), 
    slider = $('<div id="gallerySlider">'), 
    prevArrow = $('<a id="prevArrow"></a>'), 
    nextArrow = $('<a id="nextArrow"></a>'), 
    overlayVisible = false; 
var gtritems = [];  

/* Creating the plugin */ 

$.fn.touchTouch = function(){ 

    var placeholders = $([]), 
     index = 0, 
     items = this; 

    // Appending the markup to the page 
    overlay.hide().appendTo('body'); 
    slider.appendTo(overlay); 

    // Creating a placeholder for each image 
    items.each(function(){ 
     placeholders = placeholders.add($('<div class="placeholder">')); 
    }); 

    $("#gallerySlider").empty(); 
    // Hide the gallery if the background is touched/clicked 
    slider.append(placeholders).on('click',function(e){ 
     if(!$(e.target).is('img')){ 
      hideOverlay(); 
     } 
    }); 

    // Listen for touch events on the body and check if they 
    // originated in #gallerySlider img - the images in the slider. 
    $('body').on('touchstart', '#gallerySlider img', function(e){ 

     var touch = e.originalEvent, 
      startX = touch.changedTouches[0].pageX; 

     slider.on('touchmove',function(e){ 

      e.preventDefault(); 

      touch = e.originalEvent.touches[0] || 
        e.originalEvent.changedTouches[0]; 

      if(touch.pageX - startX > 10){ 
       slider.off('touchmove'); 
       showPrevious(); 
      } 
      else if (touch.pageX - startX < -10){ 
       slider.off('touchmove'); 
       showNext(); 
      } 
     }); 

     // Return false to prevent image 
     // highlighting on Android 
     return false; 

    }).on('touchend',function(){ 
     slider.off('touchmove'); 
    }); 

    // Listening for clicks on the thumbnails 

    items.on('click', function(e){ 
     e.preventDefault(); 

     // Find the position of this image 
     // in the collection 

     function Gtr(obj, href, ind, itemind) { 
      this.obj=obj; 
      this.href=href; 
      this.ind=ind; 
      this.itemind=itemind; 
      return this; 
     } 
     gtritems = []; 
     var i = 0; 
     var oldindex = items.index(this); 
     $.each(items, function() { 
      if(window.selector == '*'){ 
       gtritems.push(new Gtr(this, items.eq(items.index(this)).attr('href'), items.index(this), items.index(this))); 
       if(oldindex == items.index(this)) { 
        index = oldindex; 
       } 
      } else { 
       if($(this).attr("class").split(" ",4)[3] == window.selector.substr(1)){ 
        gtritems.push(new Gtr(this, items.eq(items.index(this)).attr('href'), i, items.index(this))); 
        if(oldindex == items.index(this)) { 
         index = i; 
        } 
        i++; 
       } 
      } 
     }); 
    ... 
0

我找到了一種方法來製作工作導航,它使用了帳戶過濾器,但是現在我遇到了問題,即在第一次顯示投資組合時 - 在進行任何過濾之前 - 觸摸觸摸插件不會觸發。

在這裏,我告訴你有關的代碼:

Gallery.php(模板覆蓋的部分):

<?php if($this->params->get('show_filter')): ?> 

<script type="text/javascript"> 
    jQuery(document).ready(function() { 
    (function($){ 
    $(window).load(function(){ 

    var $container = $('#isotopeContainer'); 

    // filter items when filter link is clicked 
    $('#filters a').click(function(){ 
     var selector = $(this).attr('data-filter'); 
     $container.isotope({ filter: selector }); 
     if(selector == "*"){ 
     console.log(selector); 
     } else { 
     $(".touchGalleryLink").removeClass("MyCat"); 
     $(".touchGalleryLink").each(function() { 
      var tmpvar = $(this).parent().parent().attr('class').split(" ",2); 
      if(tmpvar[1] == selector.substr(1)){ 
       $(this).addClass("MyCat"); 
      } 
     }); 
     } 
     jQuery('a.touchGalleryLink.MyCat').touchTouch(); 
     return false; 
    }); 
... 

在文件的script.js我需要註釋掉行:

jQuery(document).ready(function() { 
    ... 
    // Initialize the gallery touch 
    //jQuery('a.touchGalleryLink').touchTouch(); 
    ... 

的信息,touch.galery.js文件的繼承人的一部分:

(function($){ 

/* Private variables */ 

var overlay = $('<div id="galleryOverlay">'), 
slider = $('<div id="gallerySlider">'), 
prevArrow = $('<a id="prevArrow"></a>'), 
nextArrow = $('<a id="nextArrow"></a>'), 
overlayVisible = false; 

/* Creating the plugin */ 

$.fn.touchTouch = function(){ 

    var placeholders = $([]), 
    index = 0, 
    items = this; 
    // Appending the markup to the page 
    overlay.hide().appendTo('body'); 
    slider.appendTo(overlay); 

    // Creating a placeholder for each image 
    items.each(function(){ 
    placeholders = placeholders.add($('<div class="placeholder">')); 
}); 

... 

如果有人可以幫助我找到一種方法,使其也工作的第一次,這將是不錯...

0

在這一點上,我幾乎得到了我想要的,但有一個錯誤在我的代碼和我看不到它從哪裏來。

我選擇的解決方案是引入一個新的MyCat類,我的修改試圖確保只有當圖像對應於當前類別時才存在此類,並且jquery考慮了此類。

下面是對這個錯誤的描述: 這種情況發生在我第一次使用x圖像過濾某個類別時,瀏覽它們並離開最後一幅圖像的導航。 然後我過濾一個小於x圖像的類別,讓我們說y。 現在,當我點擊圖像時,我沒有得到任何圖像,或者在某些情況下圖像不好),我需要點擊上一圖像鏈接(y-x)次才能得到正確的圖像。請注意,此時系統穩定下來,因爲現在下一個圖像鏈接不可用。

我希望如果不讓我知道我的描述是可以理解的。 我不知道是否有更優雅的方式來做到這一點,但因爲它幾乎可行,請讓我知道,如果你看到可以解釋錯誤的東西。

下面是我修改了兩個重要文件的代碼:

gallery.php:

... 
<?php if((!empty($this->lead_items) || (!empty($this->intro_items))) && $this->params->get('show_filter')): ?> 
     <div class="filters"> 


     <ul id="filters" class="unstyled"> 
      <li><a href="#" data-filter="*" class="selected"><?php echo JText::_('TPL_COM_CONTENT_GALLERY_FILTER_SHOW_ALL'); ?></a></li> 
      <?php 
      foreach ($galleryCategories as $key => $value) : 
      ?> 
      <li><a class="" href="#"data-filter=".<?php echo special_chars_replace($value); ?>"><?php echo ucwords(str_replace("_"," ",$value)); ?></a></li> 
      <?php endforeach; ?> 
     </ul> 

     <div class="clearfix"></div> 
     </div> 
<?php endif; ?> 
... 
<div class="row-fluid"> 
    <ul id="isotopeContainer" class="gallery items-row cols-<?php echo (int) $this->columns;?>"> 
    <?php 
     $valgtr = array(); 
     $keysgtr = array_keys($this->intro_items); 
     shuffle($keysgtr); 
     foreach($keysgtr as $keygtr) $valgtr[] = $this->intro_items[$keygtr]; 

     foreach ($valgtr as $key => &$item) : 
    ?> 
    <?php 
     $key = ($key - $leadingcount) + 1; 
     $rowcount = (((int) $key - 1) % (int) $this->columns) + 1; 

     if ($rowcount == 1) : ?>  
     <?php endif; ?> 

     <li class="gallery-item <?php echo special_chars_replace(strtolower(str_replace(" ","_",$item->category_title))); ?>"> 
      <?php 
      $this->item = &$item; 
      echo $this->loadTemplate('item'); 
... 
<script type="text/javascript"> 
    jQuery(document).ready(function() { 
    (function($){ 
    $(window).load(function(){ 
    var $cols = <?php echo $this->columns; ?>; 
    var $container = $('#isotopeContainer'); 

    $item = $('.gallery-item') 
    $item.outerWidth(Math.floor($container.width()/$cols)); 

    $container.isotope({ 
     animationEngine: 'best-available', 
     animationOptions: { 
      queue: false, 
      duration: 800 
     }, 
     containerClass : 'isotope', 
     containerStyle: { 
      position: 'relative', 
      overflow: 'hidden' 
     }, 
     hiddenClass : 'isotope-hidden', 
     itemClass : 'isotope-item', 
     resizable: true, 
     resizesContainer : true, 
     transformsEnabled: !$.browser.opera // disable transforms in Opera 
    }); 

    if($container.width() <= '767'){ 
     $item.outerWidth($container.width()); 
     $item.addClass('straightDown'); 
     $container.isotope({ 
     layoutMode: 'straightDown' 
     }); 
    } else { 
     $item.removeClass('straightDown'); 
     $container.isotope({ 
     layoutMode: 'fitRows' 
     }); 
    } 

    $(window).resize(function(){ 
     $item.outerWidth(Math.floor($container.width()/$cols)); 
     if($container.width() <= '767'){ 
     $item.outerWidth($container.width()); 
     $item.addClass('straightDown'); 
     $container.isotope({ 
      layoutMode: 'straightDown' 
     }); 
     } else { 
     $item.outerWidth(Math.floor($container.width()/$cols)); 
     $item.removeClass('straightDown'); 
     $container.isotope({ 
      layoutMode: 'fitRows' 
     }); 
     } 
    }); 
    }); 
})(jQuery); 
}); 
</script> 

<?php if($this->params->get('show_filter')): ?> 

<script type="text/javascript"> 
    jQuery(document).ready(function() { 
    (function($){ 
    $(window).load(function(){ 
    var $container = $('#isotopeContainer'); 

    // filter items when filter link is clicked 
    $('#filters a').click(function(){ 
     var selector = $(this).attr('data-filter'); 
     $container.isotope({ filter: selector }); 
     if(selector == "*"){ 
     $(".touchGalleryLink").addClass("MyCat"); 
     } else { 
     $(".touchGalleryLink").removeClass("MyCat"); 
     $(".touchGalleryLink").each(function() { 
      var tmpvar = $(this).parent().parent().attr('class').split(" ",2); 
      if(tmpvar[1] == selector.substr(1)){ 
      $(this).addClass("MyCat"); 
     } 
     }); 
    } 
    jQuery('a.touchGalleryLink.MyCat').touchTouch(); 
    return false; 
    }); 
    $("#filters a:first").trigger("click"); 

    var $optionSets = $('#filters li'), 
     $optionLinks = $optionSets.find('a'); 

     $optionLinks.click(function(){ 
      var $this = $(this); 
      // don't proceed if already selected 
      if ($this.hasClass('selected')) { 
      return false; 
      } 
      var $optionSet = $this.parents('#filters'); 
      $optionSet.find('.selected').removeClass('selected'); 
      $this.addClass('selected'); 

      // make option object dynamically, i.e. { filter: '.my-filter-class' } 
      var options = {}, 
       key = $optionSet.attr('data-option-key'), 
       value = $this.attr('data-option-value'); 
      // parse 'false' as false boolean 
      value = value === 'false' ? false : value; 
      options[ key ] = value; 
      if (key === 'layoutMode' && typeof changeLayoutMode === 'function') { 
      // changes in layout modes need extra logic 
      changeLayoutMode($this, options) 
      } else { 
      // otherwise, apply new options 
      $container.isotope(options); 
      } 

      return false; 
     }); 
    }); 
})(jQuery); 
}); 
</script> 

<?php endif; ?> 

touch.gallery.js:

(function($){ 

    /* Private variables */ 
    var overlay = $('<div id="galleryOverlay">'), 
    slider = $('<div id="gallerySlider">'), 
    prevArrow = $('<a id="prevArrow"></a>'), 
    nextArrow = $('<a id="nextArrow"></a>'), 
    overlayVisible = false; 


/* Creating the plugin */ 

$.fn.touchTouch = function(){ 

    var placeholders = $([]), 
     index = 0, 
     items = this; 
     gtritems = items; 
     $.each(gtritems, function() { 
      if(!$(this).hasClass("MyCat")){ 
      $(this).remove(); 
      } 
     }); 
     console.log(gtritems); 
    // Appending the markup to the page 
    overlay.hide().appendTo('body'); 
    slider.appendTo(overlay); 

    // Creating a placeholder for each image 
    gtritems.each(function(){ 
     placeholders = placeholders.add($('<div class="placeholder">')); 
    }); 

    $("#gallerySlider").empty(); 
    // Hide the gallery if the background is touched/clicked 
    slider.append(placeholders).on('click',function(e){ 
     if(!$(e.target).is('img')){ 
     hideOverlay(); 
     } 
    }); 

    // Listen for touch events on the body and check if they 
    // originated in #gallerySlider img - the images in the slider. 
    $('body').on('touchstart', '#gallerySlider img', function(e){ 

     var touch = e.originalEvent, 
     startX = touch.changedTouches[0].pageX; 

     slider.on('touchmove',function(e){ 

     e.preventDefault(); 

     touch = e.originalEvent.touches[0] || 
      e.originalEvent.changedTouches[0]; 

     if(touch.pageX - startX > 10){ 
      slider.off('touchmove'); 
      showPrevious(); 
     } 
     else if (touch.pageX - startX < -10){ 
      slider.off('touchmove'); 
      showNext(); 
      } 
     }); 

     // Return false to prevent image 
     // highlighting on Android 
     return false; 

    }).on('touchend',function(){ 
     slider.off('touchmove'); 
    }); 

    // Listening for clicks on the thumbnails 

    gtritems.on('click', function(e){ 
     e.preventDefault(); 

     // Find the position of this image 
     // in the collection 

     index = gtritems.index(this); 
     console.log(this); 
     showOverlay(index); 
     showImage(index); 

     // Preload the next image 
     preload(index+1); 

     // Preload the previous 
     preload(index-1); 

    }); 

    // If the browser does not have support 
    // for touch, display the arrows 
    if (!("ontouchstart" in window)){ 
     overlay.append(prevArrow).append(nextArrow); 

     prevArrow.click(function(e){ 
     e.preventDefault(); 
     showPrevious(); 
     }); 

     nextArrow.click(function(e){ 
     e.preventDefault(); 
     showNext(); 
     }); 
    } 

    // Listen for arrow keys 
    $(window).bind('keydown', function(e){ 

     if (e.keyCode == 37){ 
     showPrevious(); 
     } 
     else if (e.keyCode==39){ 
     showNext(); 
     } 

    }); 


    /* Private functions */ 


    function showOverlay(index){ 

     // If the overlay is already shown, exit 
     if (overlayVisible){ 
     return false; 
     } 

     // Show the overlay 
     overlay.show(); 

     setTimeout(function(){ 
     // Trigger the opacity CSS transition 
     overlay.addClass('visible'); 
     }, 100); 

     // Move the slider to the correct image 
     offsetSlider(index); 

     // Raise the visible flag 
     overlayVisible = true; 
    } 

    function hideOverlay(){ 
     // If the overlay is not shown, exit 
     if(!overlayVisible){ 
     return false; 
     } 

     // Hide the overlay 
     overlay.hide().removeClass('visible'); 
     overlayVisible = false; 
    } 

    function offsetSlider(index){ 
     // This will trigger a smooth css transition 
     slider.css('left',(-index*100)+'%'); 
    } 

    // Preload an image by its index in the items array 
    function preload(index){ 
     setTimeout(function(){ 
     showImage(index); 
     }, 1000); 
    } 

    // Show image in the slider 
    function showImage(index){ 

     // If the index is outside the bonds of the array 
     if(index < 0 || index >= gtritems.length){ 
     return false; 
     } 

     // Call the load function with the href attribute of the item 
     loadImage(gtritems.eq(index).attr('href'), function(){ 
     placeholders.eq(index).html(this); 
     }); 
    } 

    // Load the image and execute a callback function. 
    // Returns a jQuery object 

    function loadImage(src, callback){ 
     var img = $('<img>').on('load', function(){ 
     callback.call(img); 
     }); 

     img.attr('src',src); 
     } 

     function showNext(){ 

     // If this is not the last image 
     if(index+1 < gtritems.length){ 
      index++; 
      offsetSlider(index); 
      preload(index+1); 
     } 
     else{ 
      // Trigger the spring animation 

      slider.addClass('rightSpring'); 
      setTimeout(function(){ 
      slider.removeClass('rightSpring'); 
      },500); 
     } 
     } 

    function showPrevious(){ 

     // If this is not the first image 
     if(index>0){ 
     index--; 
     offsetSlider(index); 
     preload(index-1); 
     } 
     else{ 
     // Trigger the spring animation 

     slider.addClass('leftSpring'); 
     setTimeout(function(){ 
      slider.removeClass('leftSpring'); 
     },500); 
     } 
    } 
}; 

})(jQuery); 
0

我肯定不是jQuery的專家 爲了我的理解在這個時候我認爲我的解決方案的錯誤來自於這樣的事實,我必須將觸摸觸摸功能的調用從script.js文件加載1 ti我在頁面加載時過濾器點擊功能在過濾器發生變化時執行。 因爲我觀察到控制檯中的變量gtritems,並且可以看到每次我過濾時我都添加了一些新的事件,並且所有這些積累似乎都會引發錯誤。 有沒有辦法清理舊的出現次數,這樣,當我打電話:

jQuery('a.touchGalleryLink.MyCat').touchTouch(); 

有一個新的乾淨的代碼執行

? 有些幫助真的會被讚賞!