为什么我的旋转木马会在用户交互中快速到达顶部

Why is my carousel snapping to the top on user interaction?

本文关键字:顶部 交互 旋转木马 我的 用户 为什么      更新时间:2023-09-26

我在我的单页网站上做了一个旋转展示,展示了我的一些摄影作品。我遇到的问题是,每当我点击其中一个箭头访问下一张图片-左边或右边,我立即被抓拍到页面的顶部!

我已经检查了CSS和JS,我似乎找不到任何原因,为什么会发生这种情况。

HTML:

<div class="container">
  <h3><span class="underline">Exploration</span></h3>
    <p>I love exploring and capturing epic moments on my journeys. Here's some of my favourites from my latest trip across the west coast of America.</p>
</div>    
<div class="slider">
<!--SLIDE 1 START-->
      <div class="slide active-slide slide-feature slide-feature-1">
        <div class="container">
          <div class="row">
          </div>
        </div>
      </div> 
<!--SLIDE 1 END-->
<!--SLIDE 2 START-->
      <div class="slide slide-feature slide-feature-2">
        <div class="container">
          <div class="row">
          </div>
        </div>
      </div> 
<!--SLIDE 2 END-->
<!--SLIDE 3 START-->
      <div class="slide slide-feature slide-feature-3">
        <div class="container">
          <div class="row">
          </div>
        </div>
      </div> 
<!--SLIDE 3 END-->
<!--SLIDE 4 START-->    
      <div class="slide slide-feature slide-feature-4">
        <div class="container">
          <div class="row">
          </div>
        </div>
      </div> 
<!--SLIDE 4 END-->
<!--SLIDE 5 START-->
      <div class="slide slide-feature slide-feature-5">
        <div class="container">
          <div class="row">
          </div>
        </div>
      </div> 
<!--SLIDE 5 END-->     
</div>

    <div class="slider-nav">
      <a href="#" class="arrow-prev"><img src="images/arrow-left.svg"></a>
      <ul class="slider-dots">
        <li class="dot active-dot">&bull;</li>
        <li class="dot">&bull;</li>
        <li class="dot">&bull;</li>
        <li class="dot">&bull;</li>
        <li class="dot">&bull;</li>
      </ul>
      <a href="#" class="arrow-next"><img src="images/arrow-right.svg"></a>
    </div>
</div>
<!--FLIPBOARD ENDS HERE-->  
</div>
CSS:

.exploration {
    height: 1100px;
    background-color: #ffffff;
}
.exploration .container {
    position: relative;
    top: 35px;
    width: 1200px;
}
.exploration h3 {
  color: #313131;
  font-size: 40px;  
  font-family: 'Oswald', sans-serif;
  font-weight: 300;
  padding-bottom: 20px;
  text-align: center;
}
.exploration p {
    color: #313131;
    font-size: 20px;
    font-family: 'Oswald', sans-serif;
    font-weight: 300;
    text-align: center;
}

.slider {
  position: relative;
  top: 50px;  
  width: 100%;
  height: 800px;
}
.slide {
  display: none;
  width: 100%;
  height: 100%;
}
.active-slide {
    display: block;
}
/* Slide feature */
.slide-feature { 
    text-align: center;
    height: 800px;
}
.slide-feature-1 {
  background-image: url('https://scontent-lhr.xx.fbcdn.net/hphotos-xaf1/t31.0-8/11036160_10152854777396270_5157414753497878003_o.jpg');
  background-position: center;
}
.slide-feature-2 {
  background-image: url('https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xta1/t31.0-8/11218515_10152909922431270_7749144937209461633_o.jpg');
  background-position: center;
}
.slide-feature-3 {
  background-image: url('https://scontent-lhr.xx.fbcdn.net/hphotos-xpa1/t31.0-8/11187795_10152891725491270_1769195601160147349_o.jpg');
  background-position: bottom;
}
.slide-feature-4 {
  background-image: url('https://scontent-lhr.xx.fbcdn.net/hphotos-xaf1/t31.0-8/11154672_10152854784061270_3532862830070230799_o.jpg');
  background-position: center;
}
.slide-feature-5 {
  background-image: url('https://scontent-lhr.xx.fbcdn.net/hphotos-xap1/t31.0-8/11164749_10152909922426270_8192461025609874418_o.jpg');
  background-position: center;  
}
.slide-feature img {
  margin-top: 112px;
  margin-bottom: 28px;
}
.slider-nav {
  text-align: center;
  margin-top: 20px;
}
.arrow-prev {
  margin-right: 45px;
  display: inline-block;
  vertical-align: top;
  margin-top: 9px;
  position: relative;
  top: 40px;
}
.arrow-next {
  margin-left: 45px;
  display: inline-block;
  vertical-align: top;
  margin-top: 9px;
  position: relative;
  top: 40px;
}
.slider-dots {
  list-style: none;
  display: inline-block;
  padding-left: 0;
  margin-bottom: 0;
  position: relative;
  top: 40px;
}
.slider-dots li {
  color: #bbbcbc;
  display: inline;
  font-size: 30px;
  margin-right: 5px;
}
.slider-dots li.active-dot {
  color: #7FCCE5;
}

JS:

var main = function() {
$('.dropdown-toggle').click(function() {
    $('.dropdown-menu').toggle();
});
//Next Arrow Functionality    
$('.arrow-next').click(function() {
    var currentSlide=$('.active-slide');
    var nextSlide=currentSlide.next();
    var currentDot=$('.active-dot');
    var nextDot=currentDot.next();
    if (nextSlide.length == 0) {
        nextSlide=$('.slide').first();
        nextDot=$('.dot').first();
    }
    currentSlide.fadeOut(600).removeClass('active-slide');
    nextSlide.fadeIn(600).addClass('active-slide');
    currentDot.removeClass('active-dot');
    nextDot.addClass('active-dot');
});
//Previous Arrow Click Functionality
$('.arrow-prev').click(function() {
    var currentSlide=$('.active-slide');
    var prevSlide=currentSlide.prev();
    var currentDot=$('.active-dot');
    var prevDot=currentDot.prev();
    if(prevSlide.length == 0) {
        prevSlide=$('.slide').last(); 
        prevDot=$('.dot').last();
    }

    currentSlide.fadeOut(600).removeClass('active-slide');
    prevSlide.fadeIn(600).addClass('active-slide');
    currentDot.removeClass('active-dot');
    prevDot.addClass('active-dot');
    });

//Load Jumbotron text on page open.
$("#test h1").addClass("load");
};
$(document).ready(main);

您需要将e.preventDefault();添加到您的onclick函数

检查小提琴

编辑 正如我刚刚在评论部分评论的那样,是href="#"导致页面跳转到顶部。因此,从技术上讲,如果您删除achor标签,e.preventDefault();是不必要的。但最好还是留着。

添加一个参数(e)到你的点击回调函数,然后防止默认发布(一个锚标记与href设置),这一行:

e.preventDefault();

:

        //Next Arrow Functionality
        $('.arrow-next').click(function (e) {
            var currentSlide = $('.active-slide');
            var nextSlide = currentSlide.next();
            var currentDot = $('.active-dot');
            var nextDot = currentDot.next();
            if (nextSlide.length == 0) {
                nextSlide = $('.slide').first();
                nextDot = $('.dot').first();
            }
            currentSlide.fadeOut(600).removeClass('active-slide');
            nextSlide.fadeIn(600).addClass('active-slide');
            currentDot.removeClass('active-dot');
            nextDot.addClass('active-dot');
            e.preventDefault();
        });
        //Previous Arrow Click Functionality
        $('.arrow-prev').click(function (e) {
            var currentSlide = $('.active-slide');
            var prevSlide = currentSlide.prev();
            var currentDot = $('.active-dot');
            var prevDot = currentDot.prev();
            if (prevSlide.length == 0) {
                prevSlide = $('.slide').last();
                prevDot = $('.dot').last();
            }

            currentSlide.fadeOut(600).removeClass('active-slide');
            prevSlide.fadeIn(600).addClass('active-slide');
            currentDot.removeClass('active-dot');
            prevDot.addClass('active-dot');
            e.preventDefault();
        });