使用js中的css类在一定次数的点击后重定向

时间:2017-09-05 20:14:28

标签: javascript jquery mysql

我试图扩展以前的js代码,你们过去一直帮助我。该代码在网页上创建汽车幻灯片及其定价,从mysql数据库中提取数据。我让它在一段时间后重定向到新页面的地方工作,但现在我希望重定向发生时,总播放次数等于幻灯片的数量* 2.我没有经验js和任何帮助将太棒了......

以下是创建幻灯片的代码...(jscript.js)

    $(document).ready(function(){
      var currentPosition = 0;
      var slideWidth = 1280;
      var slides = $('.slide');
      var numberOfSlides = slides.length;
      // Remove scrollbar in JS
      $('#slidesContainer').css('overflow', 'hidden');
      // Wrap all .slides with #slideInner div
      slides
        .wrapAll('<div id="slideInner"></div>')
        // Float left to display horizontally, readjust .slides width
        .css({
          'float' : 'left',
          'width' : slideWidth
        });
      // Set #slideInner width equal to total width of all slides
      $('#slideInner').css('width', slideWidth * numberOfSlides);
      // Insert controls in the DOM
      $('#slideshow')
        .prepend('<span class="control" id="leftControl"></span>')
        .append('<span class="control" id="rightControl"></span>');
      // Hide left arrow control on first load
      manageControls(currentPosition);
      // Create event listeners for .controls clicks
      $('.control')
        .bind('click', function(){
        // Determine new position
        currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
        // Hide / show controls
        manageControls(currentPosition);
        // Move slideInner using margin-left
        $('#slideInner').animate({
        'marginLeft' : slideWidth*(-currentPosition)
            }, function() {
            // if last slide then move the pointer to 1st slide
            if(currentPosition == numberOfSlides-1) {
                currentPosition = -1;
            }
        });
      });

      window.setInterval(function() {
        $('#rightControl.control').click();
        }, 3000);

      // manageControls: Hides and Shows controls depending on currentPosition
      function manageControls(position){
        // Hide left arrow if position is first slide
        if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
        // Hide right arrow if position is last slide
        if(position==numberOfSlides-1){ $('#rightControl').hide() } else{$('#rightControl').show() }
      } 

    });

我认为重定向的代码类似于......(nextpage.js)

    $(document).ready(function(){
      var currentPosition = 0;
      var slides = $('.slide');
      var numberOfSlides = slides.length;
      var clicks = $('.control');
      var slidesPlayed = clicks.length;

      if(numberOfSlides == (slidesPlayed * 2)){
        window.location.href = "https://www.cars.com";
        }

      });

非常感谢任何帮助 谢谢!

1 个答案:

答案 0 :(得分:1)

新鲜测试:

$(document).ready(function(){
      var slidesPlayed = 0;
      var whatever = 5;  //that would be the total slides * 2 if I understand what you want, but let's say it's 5 for this example
      $('#thetrigger').on("click", function() {
          slidesPlayed++;

          console.log(slidesPlayed);

          if(slidesPlayed >= whatever){
              window.location.href = "https://www.cars.com";
          }
      });
});

如果您随时点击#trigger,您将被重定向。