Spo*_*ger 1 javascript jquery jquery-click-event
所以我在我的页面上运行了jQuery滚动.但我想改变一件事.我不想在网址中显示锚文本.即.#products或#contactinfo
HTML代码:
<div>
<a href="#products">Products</a>
<a href="#contactinfo">Contact info</a>
</div>
<div id="products"></div>
<div id="contactinfo"></div>
Run Code Online (Sandbox Code Playgroud)
jQuery代码:
$(document).ready(function(){
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 900, 'swing', function () {
window.location.hash = target;
});
});
});
Run Code Online (Sandbox Code Playgroud)
试试这个:
HTML:
<div>
<span class="link" rel="#products">Products</span>
<span class="link" rel="#contactinfo">Contact info</span>
</div>
<div style="height:800px;"></div>
<div id="products">products</div>
<div style="height:800px;"></div>
<div id="contactinfo">contact</div>
<div style="height:800px;"></div>
Run Code Online (Sandbox Code Playgroud)
脚本:
$(document).ready(function(){
$('.link').on('click',function (e) {
$('html, body').stop().animate({
'scrollTop': $($(this).attr('rel')).offset().top
}, 900, 'swing', function () {});
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3091 次 |
| 最近记录: |