【发布时间】:2019-08-08 01:16:58
【问题描述】:
我想制作一个类似这个网站上的滚动按钮: https://codepad.co/snippet/scroll-down-button-css 我已经复制了所有代码,但是当我在浏览器中打开网站时,按钮不起作用,即使我逐字复制了所有内容。我不知道足够的JS来解决这个问题。感谢您的帮助。
【问题讨论】:
标签: javascript css button scroll
我想制作一个类似这个网站上的滚动按钮: https://codepad.co/snippet/scroll-down-button-css 我已经复制了所有代码,但是当我在浏览器中打开网站时,按钮不起作用,即使我逐字复制了所有内容。我不知道足够的JS来解决这个问题。感谢您的帮助。
【问题讨论】:
标签: javascript css button scroll
你说你一个字一个字地复制了所有的东西,但是,你的笔记本电脑上不工作,只是示例代码。
或者您正在现有网站上对其进行测试??
在案例 1 中> 确保在页面中包含 jQuery 库以确保所有功能都正常工作
在案例 2 中 > 您必须更改滚动的方向,如您所见
{scrollTop: $('section.ok').offset().top }, 'slow');
就像你可以看到滚动将被定向到名为“ok”的类的部分
所以请确定你的情况并祝你好运
【讨论】:
您必须在本地使用 jquery 库文件或使用 cdn 地址。没有 jquery 滚动功能将无法工作。
jquery cdn 路径为:-http://code.jquery.com/jquery-1.12.1.min.js
只需复制此脚本标记并在您的函数之前使用。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
background-color: #000;
}
*,
:after,
:before {
box-sizing: border-box;
margin: 0;
padding: 0;
}
section {
height: 100vh;
width: 100%;
display: table;
}
section.ok{
background-color: #555;
}
p{
color: white;
font-family: arial;
display: table-cell;
text-align: center;
vertical-align: middle;
}
.scroll-down {
opacity: 1;
-webkit-transition: all .5s ease-in 3s;
transition: all .5s ease-in 3s;
}
.scroll-down {
position: absolute;
bottom: 30px;
left: 50%;
margin-left: -16px;
display: block;
width: 32px;
height: 32px;
border: 2px solid #FFF;
background-size: 14px auto;
border-radius: 50%;
z-index: 2;
-webkit-animation: bounce 2s infinite 2s;
animation: bounce 2s infinite 2s;
-webkit-transition: all .2s ease-in;
transition: all .2s ease-in;
}
.scroll-down:before {
position: absolute;
top: calc(50% - 8px);
left: calc(50% - 6px);
transform: rotate(-45deg);
display: block;
width: 12px;
height: 12px;
content: "";
border: 2px solid white;
border-width: 0px 0 2px 2px;
}
@keyframes bounce {
0%,
100%,
20%,
50%,
80% {
-webkit-transform: translateY(0);
-ms-transform: translateY(0);
transform: translateY(0);
}
40% {
-webkit-transform: translateY(-10px);
-ms-transform: translateY(-10px);
transform: translateY(-10px);
}
60% {
-webkit-transform: translateY(-5px);
-ms-transform: translateY(-5px);
transform: translateY(-5px);
}
}
</style>
</head>
<body>
<section>
<p>SCROLL DOWN CSS</p>
<a href="#" class="scroll-down" address="true"></a>
</section>
<section class="ok">
<p>OK SCROLL !</p>
</section>
<script src="http://code.jquery.com/jquery-1.12.1.min.js"></script>
<script>
$(function() {
$('.scroll-down').click (function() {
$('html, body').animate({scrollTop: $('section.ok').offset().top }, 'slow');
return false;
});
});
</script>
</body>
</html>
【讨论】: