加载时显示滑块

时间:2017-04-28 17:47:54

标签: jquery slider load

我有3个链接,允许我在点击链接时滑动页面。

但是当加载页面时,显示空页面,我喜欢加载id #one蓝页而不是空页面。

在点击任何其他链接之前,有没有人可以帮我如何加载第一页!

$(function () {

    // get the width of the first content box
    // add a bit of extra so we end up with "-350px"
    var contentWidth = '-' + ($('.content').width() + 50) + 'px';

    // reposition the content here in case javascript is disabled
    $('.content').css({
        position: 'absolute',
        left: contentWidth
    });

    $("li a").click(function () {
        event.preventDefault();
        var $blockID = $( $(this).attr('href') );
        // if the content is already showing, don't do anything
        if ($blockID.hasClass('visible')) { return; }
        // hide any visible content
        $('.content.visible')
            .removeClass('visible')
            // move the old content past the current window width, then reset it's position
            .animate({
                left: '-' + $(window).width()
            }, function () {
                // Remove left setting after the animation completes
                $(this).css('left', contentWidth);
            });
        $blockID
            .addClass('visible')
            .animate({ left: 0 });
    });

});

请参阅jsfiddle上的样本......许多人都知道。

1 个答案:

答案 0 :(得分:0)

我已将动画移动到某个函数,以便在页面加载时调用它,并使用较小的超时来创建初始延迟。

$(function() {
  setTimeout(function() {
    showDiv($("a[href='#one']"));
  }, 100);

  // get the width of the first content box
  // add a bit of extra so we end up with "-350px"
  var contentWidth = '-' + ($('.content').width() + 50) + 'px';

  // reposition the content here in case javascript is disabled
  $('.content').css({
    position: 'absolute',
    left: contentWidth
  });

  $("li a").click(function() {
    event.preventDefault();
    showDiv($(this));
  });

  function showDiv($this) {
    var $blockID = $($this.attr('href'));
    // if the content is already showing, don't do anything
    if ($blockID.hasClass('visible')) {
      return;
    }
    // hide any visible content
    $('.content.visible')
      .removeClass('visible')
      // move the old content past the current window width, then reset it's position
      .animate({
        left: '-' + $(window).width()
      }, function() {
        // Remove left setting after the animation completes
        $(this).css('left', contentWidth);
      });
    $blockID
      .addClass('visible')
      .animate({
        left: 0
      });
  }

});
.wrapper {
  position: relative;
}

.content {
  width: 300px;
  height: 300px;
  padding: 0;
  left: 0;
  top: 0;
}

.box {
  width: 300px;
  height: 300px;
  opacity: 0.5;
}

#one .box {
  background: red;
}

#two .box {
  background: blue;
}

#three .box {
  background: green;
}

ul li {
  display: inline;
  padding-right: 10px;
  list-style: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li><a href="#one">One</a>
  </li>
  <li><a href="#two">Two</a>
  </li>
  <li><a href="#three">Three</a>
  </li>
</ul>
<div class="wrapper">
  <div id="one" class="content">
    <div class="box"></div>
  </div>
  <div id="two" class="content">
    <div class="box"></div>
  </div>
  <div id="three" class="content">
    <div class="box"></div>
  </div>
</div>