滚动时的进度条

时间:2015-07-23 17:51:35

标签: jquery html scroll

在滚动页面时我想要一些帮助来制作进度条。

用户将在点击下一步时,该栏将填充。

我很困惑?

我的鳕鱼。



var sections = $('.panelSection');
console.log(sections);
var i = 0;
var scrolto = 0;

function next() {
    if (i == 0) {
        $('.prev-section').show();
    }
    
    if (i < sections.length -1) {
        i++;
        if (i == sections.length -1) {
            $('.next-section').hide();
        }
        
        $('html, body').animate({
            scrollTop: sections[i].offsetTop
        }, 500);
    } else {
        alert('end reached');
    }
}

function prev() {
    if (i == sections.length -1) {
        $('.next-section').show();
    }
    
    if (i > 0) {
        i--;
        if (i == 0) {
            $('.prev-section').hide();
        }
        
        $('html, body').animate({
            scrollTop: sections[i].offsetTop
        }, 500);
    }
}

$('html').keydown(function(e) {
    if (e.which == '38') {
        prev();
    }
    
    if (e.which == '40') {
        next();
    }
});

$('.next-section').click(function(e) {
    e.preventDefault();
    next();
});

$('.prev-section').click(function(e) {
    e.preventDefault();
    prev();
});         
&#13;
section {
    height: 100vh;
    text-align: center; 
    font-size: 30pt
}    
 
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<section class="panelSection" style="background-color:#ef0000;">
    ... section content here ...
</section>

<section class="panelSection" style="background-color:#c9d6e3;">
    ... 2nd section content here ...
</section>

<footer>
    <div class="navbar navbar-default navbar-fixed-bottom">
        <div class="container-fluid">
            <div class="navbar-collapse collapse" id="footer-body">
                <ul class="nav navbar-nav pull-left">


                </ul>

                <ul class="nav navbar-nav pull-right">

                    <a href="#" class="prev-section">up</a>
                    <a href="#" class="next-section">next</a>

                </ul>
            </div>

            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#footer-body">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>

                    <a href="#" class="next-section">next</a>


                </button>
            </div>

        </div>
    </div>
</footer>
&#13;
&#13;
&#13;

如果有人能提供帮助,我在互联网上没有想到这样的事情,我会非常感谢!

3 个答案:

答案 0 :(得分:3)

function updateProgress(num1, num2){
  var percent = Math.ceil( num1 / num2 * 100 ) + '%';
  document.getElementById('progress').style.width = percent;
}

window.addEventListener('scroll', function(){
  var top = window.scrollY;
  var height = document.body.getBoundingClientRect().height - window.innerHeight;
  updateProgress(top, height);
});
.placeholder{
  padding: 3em;
}
.progressContainer{
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 4px;
  background: gray;
}
.progress{
  height: 4px;
  background: red;
  width: 0;
  transition: width 0.5s;
}
<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title></title>
</head>

<body>
	<div class="placeholder">
		some padding...
	</div>
	<div class="placeholder">
		some padding...
	</div>
	<div class="placeholder">
		some padding...
	</div>
	<div class="placeholder">
		some padding...
	</div>
	<div class="placeholder">
		some padding...
	</div>
	<div class="placeholder">
		some padding...
	</div>
	<div class="placeholder">
		some padding...
	</div>
  <div class="progressContainer"><div id="progress" class="progress"></div></div>
</body>

</html>

答案 1 :(得分:1)

以下是我的解决方案,请查看以下实时示例:

window.onscroll = function() {
  ScrollIndicator()
};

function ScrollIndicator() {
  var winScroll = document.body.scrollTop || document.documentElement.scrollTop;
  var height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
  var scrolled = (winScroll / height) * 100;
  document.getElementById("headerBar").style.width = scrolled + "%";
  console.log(Math.round(scrolled * 100) / 100);
  document.getElementById("footerBar").style.width = scrolled + "%";
  document.getElementById("footerBar").innerHTML = Math.round(scrolled) + "%";
}
body {
  height: 2000px;
  text-align: center;
  font-family: arial;
  color: #333;
  margin: 0px;
}

.header {
  position: fixed;
  top: 0;
  z-index: 1;
  width: 100%;
  background-color: #f1f1f1;
}

.header h2 {
  text-align: center;
}

.progress-container {
  width: 100%;
  height: 8px;
  background: #ccc;
}

.progress-bar {
  height: 8px;
  background: linear-gradient(141deg, #0fb8ad 0%, #1fc8db 51%, #2cb5e8 75%);
  width: 0%;
}

.content {
  padding: 50px 0;
  margin: 50px auto 0 auto;
  width: 80%;
}

footer {
  width: 100vw;
  height: 20px;
  position: fixed;
  bottom: 0;
  left: 0;
  background: #ccc;
}

.footer-progress-bar {
  height: 20px;
  background: linear-gradient(141deg, #0fb8ad 0%, #1fc8db 51%, #2cb5e8 75%);
  width: 0%;
  text-align: center
}
<div class="header">
  <h2>Scroll Indicator</h2>
  <div class="progress-container">
    <div class="progress-bar" id="headerBar"></div>
  </div>
</div>

<div class="content">
  <h2>Scroll down to see how it works</h2>
</div>

<footer>
  <div class="footer-progress-bar" id="footerBar">0%</div>
</footer>

或使用PrognRoll jQuery插件:

<强>实施例

Body demo on CodePen

<body>
<!-- Content -->
</body>

显示滚动进度指示器:

$(function() {
  $("body").prognroll();
});

答案 2 :(得分:0)

这是一个例子, 作为奖励我添加了一个链接,你可以点击返回到该位置 https://jsbin.com/ququnovowo/edit?css,js,output

请记住,滚动时会添加许多新圈子,这可能不是预期的行为......