jq 在页面某个位置触发点击_jQuery从button onclick跳转或滚动到页面上的某个位置,div或目标...

博客围绕在页面点击按钮后跳转到特定位置展开,给出了使用jQuery实现该功能的代码示例。通过给链接添加样式使其像按钮,利用jQuery的animate方法实现滚动动画,还给出了无动画的常规跳转作为无js的备用方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When I click on a button i want to be able to jump down or scroll to a specific div or target on the page.

$('#clickMe').click(function() {

//jump to certain position or div or #target on the page

});

How can I do this?

解决方案

I would style a link to look like a button, because that way there is a no-js fallback.

So this is how you could animate the jump using jquery. No-js fallback is a normal jump without animation.

Original example:

$(document).ready(function() {

$(".jumper").on("click", function( e ) {

e.preventDefault();

$("body, html").animate({

scrollTop: $( $(this).attr('href') ).offset().top

}, 600);

});

});

#long {

height: 500px;

background-color: blue;

}

Pliip

Ploop

...
pliip
ploop

New example with actual button styles for the links, just to prove a point.

Everything is essentially the same, except that I changed the class .jumper to .button and I added css styling to make the links look like buttons.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值