从外部JavaScript导航栏中的链接平滑滚动

时间:2018-10-24 19:18:04

标签: javascript jquery html smooth-scrolling

这是我的情况。这有点不寻常,因为它是网络驱动器上的仅本地站点。根本没有设置服务器。

首先,我在a.js文件中有一个jQuery脚本,该脚本强制平滑滚动链接。这是代码:

$('a[href^="#"]').click(function () {
    $('html, body').animate({
        scrollTop: $('[name="' + $.attr(this, 'href').substr(1) + '"]').offset().top
    }, 500);

    return false;
});

如果我单击该页面上的链接,它将按预期运行:它会平滑滚动到锚点。

但是,通过调用另一个JavaScript文件,我在HTML页面上有一个HTML导航栏。如果我使用导航栏单击链接,则无法平滑滚动。它没有平滑滚动,而是直接向锚点滑动。

是否可以利用JavaScript导航栏中的jQuery脚本?

编辑:我试图添加一个有效的代码段,但是它实际上并没有用,但是它可能有助于显示这种情况。

document.write("<header id='header'>");
document.write("<div class='outer-div'><div class='mid-div'><div class='center-div'>");

document.write("<div class='dropdown'>");
  document.write("<button class='dropbtn'>HOME<i class='fas fa-chevron-down'></i></button>");
  document.write("<div class='dropdown-content'>");
    document.write("<a href='#home'>Home</a>");
    document.write("<a href='#updates'>Updates</a>");
    document.write("<a href='#calendar'>Calendar</a>");
  document.write("</div>");
document.write("</div>");

document.write("<div class='dropdown'>");
  document.write("<button class='dropbtn'>PRODUCTS<i class='fas fa-chevron-down'></i></button>");
  document.write("<div class='dropdown-content'>");
    document.write("<a href='#product1'>Product 1</a>");
    document.write("<a href='#product2'>Product 2</a>");
    document.write("<a href='#product3'>Product 3</a>");
    document.write("<a href='#product4'>Product 4</a>");
    document.write("<a href='#product5'>Product 5</a>");
  document.write("</div>");
document.write("</div>");

document.write("</div></div></div>");
document.write("</header>");

$('a[href^="#"]').click(function () {
    $('html, body').animate({
        scrollTop: $('[name="' + $.attr(this, 'href').substr(1) + '"]').offset().top
    }, 500);

    return false;
});
#header {
  position: fixed;
  left: 0px;
  top: 0;
  right: 0;
z-index: 1;
}

body {
margin: 0px;
}

.dropbtn {
height: 46px;
    background-color: white;
    color: black;
    padding: 12px;
    font-size: 28px;
    border: none;
letter-spacing: 3.3px;
margin-right: -6px;
}

@media (max-width: 1086px) {
  .dropbtn {
    font-size: 24px;
    letter-spacing: 2.8px;
  }
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: white;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    left: 5px;
    font-size: 18px;
    letter-spacing: 0px;
    min-width: 180px;
}

@media (max-width: 1086px) {
  .dropdown-content {
	font-size: 17px;
  }
}

.dropdown-content a {
    color: black;
    padding: 4px 8px;
    text-decoration: none;
    display: block;
text-align: left;
}

.dropdown-content a:hover {background-color: #cccccc;}

.dropdown:hover .dropdown-content {display: block;}


.dropbtn:hover { cursor: pointer;}



.outer-div {
background: white;
  display: table;
  position: absolute;
  height: 82px;
  width: 100%;
}
.mid-div {
background: white;
  display: table-cell;
  vertical-align: middle;
}
.center-div {
  margin: 0 auto;
  width: 100%;
  height: 82px;
text-align: center;
  background-color: white;
}

.fa-chevron-down {
font-size: 10px;
position: relative;
left: 2px;
}


.heronewbig {
  background-color: black;
  background-size: cover;
  position: relative;
}

.heronewbig .hero-containernew {
  bottom: 0;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  text-align: left;
}

.anchor {
	padding-top: 66px;
	margin-top: -66px;
}

.heronew .hero-containernew {
  position: absolute;
  bottom: 0;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  text-align: center;
}

.divstuff {
min-height: calc(100vh - 82px);
color: black;
text-align: left;
text-justify: inter-word;
margin-left: 10px;
margin-right: 10px;
display: flex;
align-items: left; 
}

.divstuffblack {
color: white;

}

.sectiontable {
width: 800px;
height: calc(100vh - 80px);
background: #;
padding-bottom: 30px;
}

p, ol, li {
font-size: 24px;
}

@media (max-width: 1086px) {
p, ol, li {
	font-size: 18px;
  }
}

h1 {
font-size: 40px;
text-transform: uppercase;
margin-bottom: -30px;
letter-spacing: 2.5px;
}

h2 {
font-size: 26px;
font-weight: bold;
text-transform: uppercase;
margin-bottom: -30px;
letter-spacing: 1.5px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>





<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css" integrity="sha384-5sAR7xN1Nv6T6+dT2mhtzEpVJvfS3NScPQTrOxhwjIuvcA67KV2R5Jz6kr4abQsz" crossorigin="anonymous">
<link href="css/newnavstyle.css" rel="stylesheet">

</head>
<body bgcolor="#cccccc">
<br><br><br>
<p><a name="product1">Product 1</a></p>
<p><a name="product2">Product 2</a></p>
<p><a name="product3">Product 3</a></p>
<p><a name="product4">Product 4</a></p>
<p><a name="product5">Product 5</a></p>





</body>
</html>

0 个答案:

没有答案