2013-02-03 51 views
1

在另一篇文章(jQuery FlexSlider hide Slider but retain visibility of Manual Controls)中,用户LJ902回答了您如何链接到Flexslider中的特定图像。它完美的工作,但只有当链接与图像位于同一页面。jQuery FlexSlider - 链接到特定图像

我想要的是:在首页上,有一堆图像缩影..当你点击其中一个,你会看到一个新的页面,在Flexslider中显示的图像,选择是用户在首页上选择的图像。

这是链接和图像的代码。注意链接中的“相对”:

<a rel="0" class="slide_thumb" href="#">slide link 1</a> 
<a rel="1" class="slide_thumb" href="#">slide link 2</a> 
<a rel="2" class="slide_thumb" href="#">slide link 3</a> 

<div class="flexslider"> 
<ul class="slides"> 
    <li> 
     <img src="demo-stuff/inacup_samoa.jpg" /> 
     <p class="flex-caption">Captions and cupcakes. Winning combination.</p> 
    </li> 
    <li> 
     <a href="http://flex.madebymufffin.com"><img src="demo-stuff/inacup_pumpkin.jpg"  /></a> 
     <p class="flex-caption">This image is wrapped in a link!</p> 
    </li> 
    <li> 
     <img src="demo-stuff/inacup_donut.jpg" /> 
    </li> 
    <li> 
     <img src="demo-stuff/inacup_vanilla.jpg" /> 
    </li> 
    <li> 
     <img src="demo-stuff/inacup_vanilla.jpg" /> 
    </li> 
    </ul> 
</div> 

这是脚本:

<script type="text/javascript" charset="utf-8"> 
jQuery(document).ready(function($) { 

$('.flexslider').flexslider({ 
    animation: "slide", 
    slideshow: false, 
    animationLoop: false, 
    directionNav: true, 
    slideToStart: 0, 
    start: function(slider) { 
     $('a.slide_thumb').click(function() { 
      $('.flexslider').show(); 
      var slideTo = $(this).attr("rel")//Grab rel value from link; 
      var slideToInt = parseInt(slideTo)//Make sure that this value is an integer; 
      if (slider.currentSlide != slideToInt) { 
       slider.flexAnimate(slideToInt)//move the slider to the correct slide (Unless the slider is also already showing the slide we want); 
      } 
     }); 
    } 

}); 

}); 

正如我所说,上述工作正常,如果链接在同一个页面作为Flexslider。

但我想的链接是一个头版,然后链接必须链接到另一个页面,如:

<a rel="0" class="slide_thumb" href="mySubpage">slide link 1</a> 

但上面的失败,因为我只得到了子页面,没有显示正确的图像(它总是显示第一个)。

如果有人可以给我一个提示如何解决这个问题,我将非常感激:)

感谢,

回答

0

你需要做的是通过之间的参数两页:

<a rel="0" class="slide_thumb" href="yourpage?img=0">slide link 1</a> 
<a rel="1" class="slide_thumb" href="yourpage?img=1">slide link 2</a> 

这里的参数的名称为IMG和值是一样的rel属性(whic你不需要了)。在目标页面上,您将添加一个获取url参数的逻辑,例如:

function getQueryParams(qs) { 
    qs = qs.split("+").join(" "); 
    var params = {}, 
     tokens, 
     re = /[?&]?([^=]+)=([^&]*)/g; 

    while (tokens = re.exec(qs)) { 
     params[decodeURIComponent(tokens[1])] 
      = decodeURIComponent(tokens[2]); 
    } 

    return params; 
} 

// assoziative object the key is the get parameter name 
var $_GET = getQueryParams(document.location.search); 

// now you only have to modify the line: 
// var slideTo = $(this).attr("rel")//Grab rel value from link; 
// like this: 
var slideTo = (($_GET['img']) ? $_GET['img'] : 0); 
// if img parameter is set, you slide to it, fallback is the first one. 
+0

我非常感谢您的回答!当然,传递一个参数是有道理的。但是我在实现这个时遇到了一些麻烦(解析错误:语法错误,意外的')',期待'&'或者T_VARIABLE)。 –

+0

@VeronicaE。这听起来像一个PHP错误,我的代码中没有PHP,它的所有js。 –

+0

我有一点麻烦。 我拥有的页面:IndexMain.php(首页),IndexSub.php(所有图像所在的子页面)和functions.php,我保存该函数。 我得到这个错误的功能页面:(解析错误:语法错误,意外的')',期待'&'或T_VARIABLE)。 IndexMain.php的内容: echo“ link 1 \t ”;“ 的functions.php: \t回声” \t变量$ _GET = getQueryParams(document.location.search); \t \t VAR startAt =(($ _GET [ 'IMG'])$ _GET [ 'IMG']: 0);“; –