【发布时间】:2017-08-08 18:09:35
【问题描述】:
我有一个返回顶部按钮,当滚动到页面顶部时应该会出现,而当它转到页面顶部时会消失。
测试时它出现在我的本地站点副本中,但在互联网上,它没有显示,只显示文本链接。
网站位于:https://www.ivanteong.com
返回顶部按钮的实现如下:
HTML 页面底部但在 JavaScript 源代码上方:
<a href="#0" class="cd-top">Top</a></div>
HTML 页面底部的 JavaScript 源代码(当我将其放入页眉时,菜单滚动出现问题):
<!-- JS -->
<script src="../js/jquery.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/waypoints.min.js"></script>
<script src="../js/counterup.min.js"></script>
<script src="../js/inview.min.js"></script>
<script src="../js/easypiechart.js"></script>
<script src="../js/magnific-popup.min.js"></script>
<script src="../js/main.js"></script>
<script src="../js/backtotop.js"></script>
main.css 中的 CSS 样式:
.cd-top {
display: inline-block;
height: 40px;
width: 40px;
position: fixed;
bottom: 40px;
right: 10px;
z-index: 1000;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.05);
/* image replacement properties */
overflow: hidden;
text-indent: 100%;
white-space: nowrap;
background: GREY url(../images/cd-top-arrow.svg) no-repeat center 50%;
visibility: hidden;
opacity: 0;
border-radius: 10px;
-webkit-transition: opacity .3s 0s, visibility 0s .3s;
-moz-transition: opacity .3s 0s, visibility 0s .3s;
transition: opacity .3s 0s, visibility 0s .3s;
}
.cd-top.cd-is-visible, .cd-top.cd-fade-out, .no-touch .cd-top:hover {
-webkit-transition: opacity .3s 0s, visibility 0s 0s;
-moz-transition: opacity .3s 0s, visibility 0s 0s;
transition: opacity .3s 0s, visibility 0s 0s;
}
.cd-top.cd-is-visible {
/* the button becomes visible */
visibility: visible;
opacity: 1;
}
.cd-top.cd-fade-out {
/* if the user keeps scrolling down, the button is out of focus and becomes less visible */
opacity: .5;
}
.no-touch .cd-top:hover {
background-color: #FF432E;
opacity: 1;
}
backtotop.js:
jQuery(document).ready(function($){
// browser window scroll (in pixels) after which the "back to top" link is shown
var offset = 0,
//browser window scroll (in pixels) after which the "back to top" link opacity is reduced
offset_opacity = 1500,
//duration of the top scrolling animation (in ms)
scroll_top_duration = 1500,
//grab the "back to top" link
$back_to_top = $('.cd-top');
//hide or show the "back to top" link
$(window).scroll(function(){
( $(this).scrollTop() > offset ) ? $back_to_top.addClass('cd-is-visible') : $back_to_top.removeClass('cd-is-visible cd-fade-out');
if( $(this).scrollTop() > offset_opacity ) {
$back_to_top.addClass('cd-fade-out');
}
});
//smooth scroll to top
$back_to_top.on('click', function(event){
event.preventDefault();
$('div,body,html').animate({
scrollTop: 0 ,
}, scroll_top_duration
);
});
});
cd-top-arrow.svg:
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 17.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="16px" height="16px" viewBox="0 0 16 16" enable-background="new 0 0 16 16" xml:space="preserve">
<polygon fill="#FFFFFF" points="8,2.8 16,10.7 13.6,13.1 8.1,7.6 2.5,13.2 0,10.7 "/>
</svg>
【问题讨论】:
-
我看到带有向上箭头的按钮,你有什么问题
-
我认为你的代码没有问题,我可以看到箭头
-
你看到了吗?你用什么浏览器?我在 Chrome 或 Safari 上的 www.ivanteong.com 上没有看到它
-
我已经检查了 firefox 和 chrome prntscr.com/g5ruma
-
chrome v60 我可以看到箭头
标签: javascript jquery html css svg