jqueryはページ上部に戻る機能を実現します.
4260 ワード
<p id="back-to-top">
<span></span>
</p>
<script type="text/javascript">
$(function() {
// #back-to-top
$("#back-to-top").hide();
// 100 , ,
$(function() {
$(window).scroll(function() {
if ($(window).scrollTop() > 100) {
$("#back-to-top").fadeIn(1500);
} else {
$("#back-to-top").fadeOut(1500);
}
});
// ,
$("#back-to-top").click(function() {
$('body,html').animate({
scrollTop : 0
}, 300);
return false;
});
});
});
</script>
<style type="text/css">
p#back-to-top {
position: fixed;
bottom: 100px;
right: 80px;
cursor:pointer;
}
p#back-to-top span {
background:url(skins/images/totop.gif) no-repeat center center;
border-radius: 6px;
display: block;
height: 80px;
width: 80px;
margin-bottom: 5px;
/* CSS3 transition <span> */
-moz-transition: background 1s;
-webkit-transition: background 1s;
-o-transition: background 1s;
}
#back-to-top span:hover {
background: url(skins/images/totop.gif) no-repeat center center;
}
</style>
jqueryライブラリの導入を忘れないでください