工作小提琴演示
试试这个我在你的小提琴中添加了一些额外的东西,点击试试
代码
$(document).ready(function () {
$("#first").addClass('scroll_item_active');
var main = main = $('#scroll');
$('.scroll_item').click(function (event) {
event.preventDefault();
var trgt = $(this).attr('id') + "1";
target_offset = $('#' + trgt).offset(),
target_top = target_offset.top;
$('html, body').animate({
scrollTop: target_top
}, 500);
main.children().removeClass('scroll_item_active');
$(this).addClass('scroll_item_active');
});
$(window).scroll(function (event) {
if ($("#first1").offset().top < $(window).scrollTop() + $(window).outerHeight()) {
$("#first").addClass('scroll_item_active');
$("#second").removeClass('scroll_item_active');
$("#third").removeClass('scroll_item_active');
$("#fourth").removeClass('scroll_item_active');
}
if ($("#second1").offset().top < $(window).scrollTop() + $(window).outerHeight()) {
$("#second").addClass('scroll_item_active');
$("#first").removeClass('scroll_item_active');
$("#third").removeClass('scroll_item_active');
$("#fourth").removeClass('scroll_item_active');
}
if ($("#third1").offset().top < $(window).scrollTop() + $(window).outerHeight()) {
$("#third").addClass('scroll_item_active');
$("#first").removeClass('scroll_item_active');
$("#second").removeClass('scroll_item_active');
$("#fourth").removeClass('scroll_item_active');
}
if ($("#fourth1").offset().top < $(window).scrollTop() + $(window).outerHeight()) {
$("#fourth").addClass('scroll_item_active');
$("#first").removeClass('scroll_item_active');
$("#second").removeClass('scroll_item_active');
$("#third").removeClass('scroll_item_active');
}
});
});
html
<div id="scroll">
<div id="first" class="scroll_item"> </div>
<div id="second" class="scroll_item"> </div>
<div id="third" class="scroll_item"> </div>
<div id="fourth" class="scroll_item"> </div>
</div>
<div id="first1"></div>
<div id="second1"></div>
<div id="third1"></div>
<div id="fourth1"></div>
css
*{
margin: 0px;
padding: 0px;
}
#scroll {
position: fixed;
top: 50%;
right: 30px;
z-index: 999;
}
.scroll_item {
border: 3px solid #FFF;
width: 10px;
height: 10px;
margin: 5px;
}
.scroll_item:hover{
background: #FFF;
}
.scroll_item_active {
border: 3px solid #FFF;
width: 10px;
height: 10px;
margin: 5px;
background-color: #FFF;
}
#first1{
width: 100%;
height: 800px;
background-color: red;
top: 0px;
}
#second1 {
width: 100%;
height: 800px;
background-color: green;
position: absolute;
top: 800px;
}
#third1{
width: 100%;
height: 800px;
background-color: blue;
position: absolute;
top: 1600px;
}
#fourth1 {
width: 100%;
height: 800px;
background-color: black;
position: absolute;
top: 2400px;
}
希望这有帮助,谢谢