首页 >> js开发 >> jsjQuery实现点击滚动到指定元素上的方法分析js大全
jsjQuery实现点击滚动到指定元素上的方法分析js大全
发布时间: 2021年1月13日 | 浏览:
| 分类:js开发
本文实例讲述了jQuery实现点击滚动到指定元素上的方法。分享给大家供大家参考,具体如下:jquery 点击滚动到指定元素上,1.获取指定元素的offset() 得到偏移位置,返回结果是top 和left
获取offset().top , offset().left 单位是像素 只对可见元素有效2.body 和html 使用animate方法的scrollTop股东到指定位置3.id参数为滚动到指定元素的ID, p_top是指定元素的margin 和padding值,这个值暂时没有意义
function scrollPosition(id,p_top) {
/*获取某个元素的相对偏移,此元素必须是可见的,返回值是top 和left 单位是像素 移动到固定元素上尽可能使用padding
*
*
*
* */
var offset= $(id).offset();
console.log(offset);
$('body,html').animate({
scrollTop:offset.top+p_top
})
};
$("#pingtai").click(function () {
scrollPosition('#pingtaijieshao','-800')
})
$("#jishu").click(function () {
scrollPosition('#jishutedian','-800')
})
function scrollPosition(id,p_top) {
/*获取某个元素的相对偏移,此元素必须是可见的,返回值是top 和left 单位是像素 移动到固定元素上尽可能使用padding
*
*
*
* */
var offset= $(id).offset();
console.log(offset);
$('body,html').animate({
scrollTop:offset.top+p_top
})
};
$("#pingtai").click(function () {
scrollPosition('#pingtaijieshao','-800')
})
$("#jishu").click(function () {
scrollPosition('#jishutedian','-800')
})
HTML绑定
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools./code/HtmlJsRun测试上述代码运行效果。在线HTML/CSS/JavaScript代码运行工具在线HTML/CSS/JavaScript代码运行工具http://tools./code/HtmlJsRun关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery页面元素操作技巧汇总》、《jQuery常见事件用法与技巧总结》、《jQuery常用插件及用法总结》、《jQuery扩展技巧总结》及《jquery选择器用法总结》jQuery页面元素操作技巧汇总jQuery常见事件用法与技巧总结jQuery常用插件及用法总结jQuery扩展技巧总结jquery选择器用法总结希望本文所述对大家jQuery程序设计有所帮助。
获取offset().top , offset().left 单位是像素 只对可见元素有效2.body 和html 使用animate方法的scrollTop股东到指定位置3.id参数为滚动到指定元素的ID, p_top是指定元素的margin 和padding值,这个值暂时没有意义
function scrollPosition(id,p_top) {
/*获取某个元素的相对偏移,此元素必须是可见的,返回值是top 和left 单位是像素 移动到固定元素上尽可能使用padding
*
*
*
* */
var offset= $(id).offset();
console.log(offset);
$('body,html').animate({
scrollTop:offset.top+p_top
})
};
$("#pingtai").click(function () {
scrollPosition('#pingtaijieshao','-800')
})
$("#jishu").click(function () {
scrollPosition('#jishutedian','-800')
})
function scrollPosition(id,p_top) {
/*获取某个元素的相对偏移,此元素必须是可见的,返回值是top 和left 单位是像素 移动到固定元素上尽可能使用padding
*
*
*
* */
var offset= $(id).offset();
console.log(offset);
$('body,html').animate({
scrollTop:offset.top+p_top
})
};
$("#pingtai").click(function () {
scrollPosition('#pingtaijieshao','-800')
})
$("#jishu").click(function () {
scrollPosition('#jishutedian','-800')
})
HTML绑定
平台介绍
技术特点
平台介绍
技术特点
感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools./code/HtmlJsRun测试上述代码运行效果。在线HTML/CSS/JavaScript代码运行工具在线HTML/CSS/JavaScript代码运行工具http://tools./code/HtmlJsRun关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery页面元素操作技巧汇总》、《jQuery常见事件用法与技巧总结》、《jQuery常用插件及用法总结》、《jQuery扩展技巧总结》及《jquery选择器用法总结》jQuery页面元素操作技巧汇总jQuery常见事件用法与技巧总结jQuery常用插件及用法总结jQuery扩展技巧总结jquery选择器用法总结希望本文所述对大家jQuery程序设计有所帮助。