2013-06-27 31 views
1

我正在創建一個網站,其中有Royal Slider HTML version。我想使用picturefill加載爲當前分辨率查看而優化的圖像。但是我有問題讓它工作。它通過正常的WP_Query爲圖像提供服務。Royalslider和Picturefill.js

我讀過這thread,但這並沒有幫助我,任何人的想法?

謝謝你,/保羅

<div id="featuredAdv" class="royalSliderAdv rsMinW"> 

    <?php 

     $args_featured_adv = array(
      'posts_per_page' => -1, 
      'orderby' => 'rand', 
      'post_type' => 'advertenties', 
      'meta_key' => 'advertentiepositie', 
      'meta_value' => 'Featured' 
     ); 

     $adv_featured = new WP_Query($args_featured_adv); 

     if($adv_featured->have_posts()): while ($adv_featured->have_posts()) : $adv_featured->the_post(); 

    ?> 

    <a href="<?php the_field('link'); ?>" target="_blank" onClick="return recordOutboundLink(this, ['<?php the_title(); ?>', '<?php the_field('link'); ?>']);"> 

     <div class="rsContent"> 

       <?php 

        $attachment_id_ft = get_field('afbeelding'); 
        $small_ft = wp_get_attachment_image_src($attachment_id_ft, 'adv-b-small-small'); // returns an array 
        $default_ft = wp_get_attachment_image_src($attachment_id_ft, 'adv-b-small-default'); // returns an array 
        $large_ft = wp_get_attachment_image_src($attachment_id_ft, 'adv-b-small-large'); // returns an array 

       ?> 

       <span data-picture data-alt="<?php the_title_attribute(array('before' => 'Photoq.nl: ', 'after' => '')); ?>"> 
        <span data-src="<?php echo $default_ft[0]; ?>"></span> 
        <span data-src="<?php echo $small_ft[0]; ?>" data-media="(min-width: 400px)"></span> 
        <span data-src="<?php echo $default_ft[0]; ?>" data-media="(min-width: 768px)"></span> 
        <span data-src="<?php echo $large_ft[0]; ?>" data-media="(min-width: 1200px)"></span> 

        <!-- Fallback content for non-JS browsers. Same img src as the initial, unqualified source element. --> 
        <noscript> 
         <img src="<?php echo $default_ft[0]; ?>" alt=""> 
        </noscript> 
       </span> 

     </div> 

    </a> 

    <?php endwhile; endif; wp_reset_query(); ?> 

</div> <!-- end #featuredAdv --> 

<script> 
    jQuery(document).ready(function($) { 
     $('#featuredAdv').royalSlider({ 
      arrowsNav: false, 
      loop: false, 
      controlsInside: false, 
      imageScaleMode: 'none', 
      imageScalePadding: 0, 
      arrowsNavAutoHide: false, 
      autoScaleSlider: true, 
      autoScaleSliderWidth: 270, 
      autoScaleSliderHeight: 572, 
      controlNavigation: 'bullets', 
      numImagesToPreload: 1, 
      thumbsFitInViewport: false, 
      navigateByClick: true, 
      startSlideId: 0, 
      autoPlay: false, 
      transitionType: 'move', 
      globalCaption: true 
     }); 
    }); 
</script> 

編輯:28-06 09:40

了與開發者聯繫......所以他給了一些提示。其中之一是確保圖片填充在滑塊之前初始化。這意味着,至少我認爲,picturefill.js需要在royalslider文件之前加載。

雖然沒有運氣。哦恩在代碼中作了一些修改:

<div class="rsIMG"> 

不遵循滑塊語法。

回答

1

我設法讓RoyalSlider今天與Picturefill2一起工作,但是您需要編輯picturefill.js文件的一部分以停止重新計算窗口大小。我只是在RoyalSlider forum中輸入了一些說明,但我也會在這裏複製以防別人幫助其他人。

首先,你必須從http://scottjehl.github.io/picturefill/#download抓住picturefill.js。 然後在文檔的HEAD中加載腳本文件,而不是在body標籤之前。 如果您需要舊瀏覽器支持,即不支持HTML5元素的瀏覽器,則需要按照說明here創建picture元素。或者,你可以在你的項目中加入Modernizr(我已經在使用modernizr了,所以我就是這麼做的)。

RoyalSlider標記: 下面是我用

<div class="royalSlider rsDefault"> <picture> <!--[if IE 9]><video style="display: none;"><![endif]--> <source srcset="img/gsa-01.jpg" media="(min-width: 768px)"> <source srcset="img/gsa-01-smallest.jpg, img/[email protected] 2x"> <!--[if IE 9]></video><![endif]--> <img class="rsImg" srcset="img/gsa-01-smallest.jpg, img/gsa-01-smallest2x.jpg 2x" alt="GSA Est. 1935"> </picture> <!-- etc... --> </div>

它看起來有點混亂在第一,但它很容易理解的代碼,它在picturefill網頁的所有解釋,但基本上第一個<source>元素應該始終是您要放置的圖像的最高質量版本,通過媒體查詢指定它應該加載的時間。因此在上面的示例< 768px中,加載了gsa-01-smallest。如果設備的像素比率> 1,則加載gsa-01-smallest2x.jpg,直到最後如果設備像素比例爲1,則加載gsa-01-smallest.jpg。 IE9包裝只是IE9支持的一個技巧。

現在,爲了同時使用RoyalSlider和圖片填充,您必須確保圖片填充已經在頁面中運行並在正確的src屬性中添加了img標籤,然後讓RoyalSlider執行此操作。爲了做到這一點,將您的平時代碼royalslider(jQuery(document).ready(function($) { $(".royalSlider").royalSlider({...)放入一個名爲defer.js的文件中。現在,只要您的文檔的最終標記之前,但顯然經過了jQuery和Royalslider JS腳本標籤,使用此代碼(被稱爲谷歌推遲js腳本):

<script type="text/javascript"> function downloadJSAtOnload() { var element = document.createElement("script"); element.src = "js/defer.js"; document.body.appendChild(element); } if (window.addEventListener) window.addEventListener("load", downloadJSAtOnload, false); else if (window.attachEvent) window.attachEvent("onload", downloadJSAtOnload); else window.onload = downloadJSAtOnload; </script>

這是說等到頁面已加載其他所有內容,然後應用該腳本(從js/defer.js加載)。

這很好地工作,直到你意識到,當你調整窗口的大小時,一切都會中斷(FML!)。這是因爲picturefill中有一段代碼,它會在每次窗口大小調整時再次換出圖像,而RoyalSlider根本不適合。因此,阻止這種情況的發生開闢picturefill.js,找到這段代碼和註釋掉(大約線250):

if(w.addEventListener){ var resizeThrottle; w.addEventListener("resize", function() { w.clearTimeout(resizeThrottle); resizeThrottle = w.setTimeout(function(){ picturefill({ reevaluate: true }); }, 60); }, false); }

這是我如何得到它的工作,它的一個很好的方式向手機提供滑塊 - 尤其是全寬滑塊。我不必在3G手機上加載〜400kb全寬圖像,我可以提供〜80kb圖像:)

相關問題