在click / ul li / jQuery上顺利滚动
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在click / ul li / jQuery上顺利滚动相关的知识,希望对你有一定的参考价值。
我在“问答”部分遇到了麻烦。就像有4个问题,那就是链接。如果单击一个,“p”将向下滑动。等等。但是当我点击这个问题时,我希望页面向下或向上滚动(如顶部)。
$(function() {
var $_openedFaq = null;
function openFaq($__p) {
$__p.slideDown();
}
function closeFaq($__p) {
$__p.slideUp();
}
$('.faq ul li').each(function() {
var $faq = $(this);
var $p = $(this).find('p');
$(this).find('a').click(function(e) {
e.preventDefault();
if ($_openedFaq != null) {
closeFaq($_openedFaq.find('p'));
$_openedFaq.find('a').removeClass("open");
}
if (($_openedFaq == null) || (($_openedFaq != null) && !$faq.is($_openedFaq))) {
openFaq($p);
$_openedFaq = $faq;
$p.prev('a').addClass("open");
} else {
$_openedFaq = null;
}
});
});
});
.faq {
background-color: #f5f5f5;
padding-top: 65px;
padding-bottom: 65px;
}
.faq div.container div.row div.col-sm-6 h1 {
font-family: 'Source Serif Pro', serif;
text-align: center;
font-size: 60px;
margin: 0;
padding-bottom: 40px;
}
.faq div.container div.row div.col-sm-6 ul {
list-style-type: none;
padding: 0;
}
.faq div.container div.row div.col-sm-6 ul li {
font-weight: bold;
font-size: 14px;
padding: 10px 10px;
padding-top: 20px;
border-bottom: 1px solid #bfbfbf;
position: relative;
}
.faq div.container div.row div.col-sm-6 ul li:last-child {
border-bottom: none;
}
.faq div.container div.row div.col-sm-6 ul li a {
text-decoration: none;
color: #4a4a4a;
padding-right: auto;
-webkit-transition: color 0.25s linear;
-o-transition: color 0.25s linear;
transition: color 0.25s linear;
}
.faq div.container div.row div.col-sm-6 ul li a:hover {
color: black;
}
.faq div.container div.row div.col-sm-6 ul li a:hover::after {
color: black;
}
.faq div.container div.row div.col-sm-6 ul li a::after {
content: "e008";
font-family: "road-trip-project-icons";
color: #f9b40a;
position: absolute;
top: 20px;
right: 0;
-webkit-transition: color 0.25s linear;
-o-transition: color 0.25s linear;
transition: color 0.25s linear;
}
.faq div.container div.row div.col-sm-6 ul li a.open {
color: black;
}
.faq div.container div.row div.col-sm-6 ul li a.open::after {
content: "e007";
font-family: "road-trip-project-icons";
color: black;
position: absolute;
top: 20px;
right: 0;
}
.faq div.container div.row div.col-sm-6 ul li p {
font-weight: normal;
display: none;
}
.faq div.container div.row div.col-sm-6 img {
width: 100%;
}
.faq {
padding-top: 200px;
height: 750px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<section class="faq">
<div class="container">
<div class="row">
<div class="col-sm-6">
<h1>Q&As</h1>
<ul>
<li>
<a href="#">Question 1</a>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.
</p>
</li>
<li>
<a href="#">Question 2</a>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.
</p>
</li>
<li>
<a href="#">Question 3</a>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.
</p>
</li>
<li>
<a href="#">Question 4</a>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.
</p>
</li>
</ul>
</div>
<div class="col-sm-6">
<img src="../includes/img/faqs.jpg" alt="Are you ready ?">
</div>
</div>
</div>
</section>
因此,当您点击“问题1”时,我希望页面滚动,直到她与问题1完全匹配。抱歉,我的英语很糟糕^^
谢谢。
答案
您可以通过以下方式收集每个元素li
的偏移顶部:
var offset = $(this).offset().top;
然后为了顺利滚动使用这个:
$('html, body').animate({
scrollTop: offset
}, 1000);
演示:
$(function() {
var $_openedFaq = null;
function openFaq($__p) {
$__p.slideDown();
}
function closeFaq($__p) {
$__p.slideUp();
}
$('.faq ul li').each(function() {
var $faq = $(this);
var $p = $(this).find('p');
var offset = $(this).offset().top;
$(this).find('a').click(function(e) {
e.preventDefault();
$('html, body').animate({
scrollTop: offset
}, 1000);
if ($_openedFaq != null) {
closeFaq($_openedFaq.find('p'));
$_openedFaq.find('a').removeClass("open");
}
if (($_openedFaq == null) || (($_openedFaq != null) && !$faq.is($_openedFaq))) {
openFaq($p);
$_openedFaq = $faq;
$p.prev('a').addClass("open");
} else {
$_openedFaq = null;
}
});
});
});
@charset "UTF-8";
@font-face {
font-family: 'Conv_Besom';
src: url("../font/Besom.eot");
src: local("☺"), url("../font/Besom.woff") format("woff"), url("../font/Besom.ttf") format("truetype"), url("../font/Besom.svg") format("svg");
font-weight: normal;
font-style: normal;
}
body {
font-family: 'Roboto', sans-serif;
letter-spacing: 1px;
font-size: 14px;
font-weight: 500;
}
section {
background-color: #f9b40a;
}
/* ################### */
/* ##### NAV BAR ##### */
/* ################### */
header {
position: fixed;
top: 0;
right: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.4);
text-transform: uppercase;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
padding-top: 20px;
padding-bottom: 20px;
z-index: 1000;
}
@media only screen and (max-width: 740px) {
header.navbar {
background-color: black;
}
}
header nav {
position: absolute;
top: 50%;
right: 0;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
margin-right: 40px;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
}
header nav ul {
list-style-type: none;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
padding: 0;
padding-left: 20px;
padding-right: 20px;
}
header nav ul+ul {
border-left: 1px solid white;
}
@media only screen and (max-width: 740px) {
header nav ul+ul {
border: none;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
}
}
header nav ul li+li {
margin-left: 25px;
}
header nav ul.menuIco li+li {
margin-left: 12px;
}
header nav ul li a {
text-decoration: none;
color: white;
-webkit-transition: color 0.25s linear;
-o-transition: color 0.25s linear;
transition: color 0.25s linear;
}
header nav ul li a:hover {
color: #f9b40a;
}
header nav ul.menuIco {
-webkit-transition: color 0.25s linear;
-o-transition: color 0.25s linear;
transition: color 0.25s linear;
}
@media only screen and (max-width: 740px) {
header nav ul.menuIco {
background-color: #f9b40a;
padding-top: 10px;
padding-bottom: 10px;
margin-top: 10px;
}
}
header nav ul.menuIco li a:hover {
color: black;
}
@media only screen and (max-width: 740px) {
header nav {
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
margin: 0;
padding: 0;
margin-top: 128px;
width: 100%;
border-top: 0.5px solid white;
z-index: 5;
background-color: black;
}
}
@media only screen and (max-width: 740px) {
header nav ul:first-child {
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: justify;
-ms-flex-pack: justify;
justify-content: space-between;
padding: 0;
padding-left: 35px;
}
}
@media only screen and (max-width: 740px) {
header nav ul:first-child li {
margin: 0;
margin-top: 10px;
margin-bottom: 10px;
}
}
@media only screen and (max-width: 740px) {
header nav ul:first-child li:first-child {
margin-top: 20px;
}
}
header h1 {
margin: 0;
margin-left: 50px;
}
header h1 a {
display: block;
text-decoration: none;
color: white;
text-indent: -6000px;
background: url("../img/rtp-inline-w.svg") no-repeat;
width: 149px;
height: 22px;
}
@media only screen and (max-width: 740px) {
header h1 {
margin-left: 30px;
}
}
/* ################## */
/* ##### SLIDER ##### */
/* ################## */
section div.slider {
background: url("../img/banner.jpg") no-repeat center;
background-size: cover;
padding-top: 127px;
padding-bottom: 65px;
color: white;
text-align: center;
font-size: 35px;
font-family: 'Source Serif Pro', serif;
margin: auto;
position: relative;
}
section div.slider::after {
content: "";
background: url("../img/eu-pin.svg");
position: absolute;
width: 29px;
height: 39px;
bottom: 40px;
right: 40px;
}
section div.slider::before {
content: "";
display: block;
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.3);
}
section div.slider div.container {
position: relative;
}
section div.slider div.container img {
width: 200px;
}
.article {
display: block;
}
/* ####################################### */
/* ##### APPLICATIONS ARE NOW CLOSED ##### */
/* ####################################### */
section div.closed {
padding-bottom: 65px;
}
section div.closed div.row div.closed--container {
margin-top: 65px;
}
section div.closed div.row div.closed--container h1 {
font-family: 'Conv_Besom';
padding: 0;
margin: 0;
margin-bottom: 50px;
font-size: 40px;
font-weight: normal;
color: white;
}
section div.closed div.row div.closed--container img {
width: 300px;
display: block;
margin: auto;
padding-bottom: 40px;
}
/* ################## */
/* ##### FOOTER ##### */
以上是关于在click / ul li / jQuery上顺利滚动的主要内容,如果未能解决你的问题,请参考以下文章