简体   繁体   English

如何使用锚链接在 div 内滚动而不滚动主页

[英]How to use anchor links to scroll within a div without scrolling the main page

I am trying to use links to scroll the content within a div .我正在尝试使用链接在div滚动内容。 HTML is here: HTML在这里:

<header>
  <div class="wrapper">
    <a href="#" class="logo"><img src="/img/header.jpg" alt="Ace Land Surveying"></a>
  </div>
  <nav>
    <div class="wrapper">
      <ul>
        <li><a href="">Home</a></li>
        <li><a href="">About Us</a></li>
        <li><a href="">Types of Surveys</a></li>
        <li><a href="">About Us</a></li>
        <li><a href="">Links</a></li>
        <li><a href="">Request a Survey</a></li>
        <li><a href="">Past Projects</a></li>
        <li><a href="">Contact</a></li>
        <li><a href="" class="last">SOQ</a></li>
      </ul>
    </div>
  </nav>
</header>
<div class="wrapper">
  <div class="content">
    <div class="column left">
      <ul class="survey-type-list">
        <li><h2><a href="#type1">CLICK HERE</a></h2></li>
        <li><h2><a href="#type2">CLICK HERE</a></h2></li>
        <li><h2><a href="#type3">CLICK HERE</a></h2></li>
      </ul>
    </div>
    <div class="column right" id="survey-column">
      <div class="survey-type" id="type1">
        <p> ... long text ... </p>
      </div>
      <div class="survey-type" id="type2">
        <p> ... long text ... </p>
      </div>
      <div class="survey-type" id="type3">
        <p> ... long text ... </p>
      </div>
    </div>
  </div>
</div>
<footer>
  <div class="footer-top">
    <div class="wrapper"></div>
  </div>
</footer>

and the CSS:和 CSS:

header {
  float: left;
  width: 100%;
  height: 200px;
}

.wrapper {
  width: 90%;
  margin: auto;
}

.content {
  float: left;
  width: 100%;
  padding: 20px;
  background: #02274b;
}

.column {
  min-height: 500px;
  padding: 20px;
  border-radius: 20px;
  color: #fff;
  background-image: -webkit-linear-gradient(top, rgba(255, 255, 255, 0.3), transparent);
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0.3), transparent);
}

.column.left {
  float: left;
  width: 20%;
}

.column.right {
  float: right;
  width: 60%;
}

.survey-type-list li {
  margin: 0 0 4px 0;
  list-style: none;
}

.survey-type-list h2 {
  font-size: 13px;
}

#survey-column {
  overflow: hidden;
  height: 460px;
}

.survey-type {
  float: left;
  width: 100%;
  height: 460px;
}

The problem is that the entire page moves with the content inside the div .问题是整个页面随着div内的内容移动。

Here is a fiddle:这是一个小提琴:

http://jsfiddle.net/q8a1s5wj/8/ http://jsfiddle.net/q8a1s5wj/8/

I tried several other threads here but none could solve my problem.我在这里尝试了其他几个线程,但没有一个可以解决我的问题。

How can I prevent the whole page from scrolling and just scroll inside the right column with my anchor links?我怎样才能防止整个页面滚动,而只是使用我的锚链接在右列内滚动?

I want to be able to click the link in the left column and see the right column scroll but not the move the entire page.我希望能够单击左列中的链接并查看右列滚动,但不能移动整个页面。

Fiddle小提琴

Some minor adaptations to the CSS - not doing this gave the wrong element offset :对 CSS 的一些小改动 - 不这样做会导致错误的元素偏移:

#survey-column {
  position: relative;
}

This one's just so the last div can scroll to the top completely :这只是为了让最后一个div可以完全滚动到顶部:

.survey-type {
  height: 520px;
}

And a bit of script to make it work :以及一些使其工作的脚本:

$(function() {

$('.column.left a').click(function() {

    var goal = $(this.hash).position().top-20,
    aim = goal+$('#survey-column').scrollTop();

    $('#survey-column').scrollTop(aim);

    return false;
});
});

The 20 pixels deduction of goal is just done to keep the same top padding as was started with... goal的 20 像素扣除只是为了保持与开始时相同的顶部填充...

您可以在顶部 div 元素上添加固定位置,我已经在 jsfiddle 上检查了您的代码并在包装器 div 元素中添加了位置样式属性

<div class="wrapper" style="position:fixed; margin-top:10px">

just add this只需添加这个

.stop-scrolling {
  height: 100%;
  overflow: hidden;
}

this class to your body tag.这个类到你的身体标签。

Reference: How to disable scrolling temporarily?参考: 如何暂时禁用滚动?

Updated Fiddle: http://jsfiddle.net/q8a1s5wj/2/更新小提琴: http : //jsfiddle.net/q8a1s5wj/2/

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

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