简体   繁体   English

如何使JQuery滑块仅更改页面刷新上的幻灯片

[英]How to make JQuery slider change slides on Page Reload only

I have an unordered list slider in a project I'm working on for a client. 我正在为客户处理的项目中有一个无序列表滑块。 Originally, the client wanted just a plain old fading slider that cycles through automatically. 最初,客户只需要一个普通的老式淡入淡出滑块即可自动循环显示。 No problem. 没问题。 Now the client is coming back to me and asking if I can make it so the slider only cycles to the next slide if a user reloads or refreshes the page. 现在客户回到我身边,问我是否可以这样做,因此,如果用户重新加载或刷新页面,则滑块只会循环到下一张幻灯片。 The original code is below: 原始代码如下:

HTML: HTML:

<ul class="fader">
    <li>Slider content goes here</li>
    <li>Slider content goes here</li>
    <li>Slider content goes here</li>
</ul>

CSS: CSS:

div.singleColumn div.leftColumn ul.fader {
    width: 100%;
    box-sizing: border-box;
    -moz-box-sizing: border-box;
    position: relative;
    height: 340px;
}

div.singleColumn div.leftColumn ul.fader li {
    padding: 0px !important;
    background: none !important;
    font-size: 10px;
}

JQuery for the original fading slider: 原始衰落滑块的JQuery:

$(function() {
    $('ul.fader li:not(:first)').hide();
    $('ul.fader li').css('position', 'absolute');
    $('ul.fader li').css('top', '0px');
    $('ul.fader li').css('left', '0px');

    var pause = false;

    function fadeNext() {
        $('ul.fader li').first().fadeOut().appendTo($('ul.fader'));
        $('ul.fader li').first().fadeIn();
    }

    $('ul.fader').hover(function() {
        pause = true;
    },function() {
        pause = false;
    });

    function doRotate() {
        if(!pause) {
          fadeNext();
        }    
    }

    var rotate = setInterval(doRotate, 5000);

});

Any help you could give me would be great. 您能给我的任何帮助都会很棒。 Thanks in advance. 提前致谢。

This is how it currently works: http://jsfiddle.net/6LJzN/ 当前是这样的: http : //jsfiddle.net/6LJzN/

You will need to use sessionStorage I guess. 我猜您将需要使用sessionStorage。

http://www.w3schools.com/html/html5_webstorage.asp http://www.w3schools.com/html/html5_webstorage.asp

So if you store a variable in sessionStorage indicating how many times the page has been viewed. 因此,如果您将变量存储在sessionStorage中,指示该页面已被查看了多少次。 Onload you can check this variable and then rotate to the appropriate view based on the variable value. 上载时,您可以检查此变量,然后根据变量值旋转到适当的视图。

Should also mention if you need to support non-html5 capable browsers then you may have to resort to a session cookie. 还应该提及如果您需要支持不支持html5的浏览器,那么您可能不得不求助于会话cookie。 Which would be very similar but the storage would be different. 这将非常相似,但存储空间将有所不同。

As you have to change the slider on the page load, it clearly means you will need to have the data that tells you that which slider you displayed in last page load. 由于您必须更改页面加载时的滑块,因此很明显,这意味着您将需要具有告诉您上次页面加载中显示了哪个滑块的数据。 That can be done by 3 ways 这可以通过3种方式完成

1) Adding into the URL as hash - Not recommended as user can try reloading without hash 1) 作为哈希添加到URL中 -不建议使用,因为用户可以尝试不使用哈希进行重新加载

2) Use cookie to save the data 2) 使用Cookie保存数据

3) Use localStorage to save the data - I used this. 3) 使用localStorage保存数据 -我使用了这个。

DEMO - http://jsfiddle.net/6LJzN/1/ 演示-http: //jsfiddle.net/6LJzN/1/

JS JS

$(function() {
    $('ul.fader li').hide();
    $('ul.fader li').css('position', 'absolute');
    $('ul.fader li').css('top', '0px');
    $('ul.fader li').css('left', '0px');

    var max = $('ul.fader li').length;

    function showSlider() {

        if(localStorage.slider) {
            $('.fader').find('li:nth('+localStorage.slider+')').fadeIn();
            localStorage.slider = parseInt(localStorage.slider,10) + 1;
            if(localStorage.slider >= max) localStorage.slider=0;
        }else{
            $('.fader').find('li:nth(0)').fadeIn();
            localStorage.slider=1;
        }
    }

    showSlider();

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM