我有这个代码工作......
myScroller.scrollTo(0, -654, 200);
我也有这个工作....
myScroller.scrollToElement("#p4", "1s");
有没有办法向#id 添加偏移量,例如 scrollToElement("#p4" + 100, "1s")?
No you can't.
myScroller.scrollToElement("#p4", "1s");
Because first parameter should be the CSS selector of a DOM element. Therefore can you use "#p4" + 100
for applying CSS rules? If your answer is YES then YOU CAN else YOU CANNOT.
If you wanted to scroll to next element after particular time what you can do is try with nth-child()
CSS property with a appropriate timeout and incremental flag. Something like this (Assume your elements are inside a parent DIV element which have id myDiv)
var incre = 0;
function customScrollToElement(){
myScroller.scrollToElement("#myDiv:nth-child(" + incre + ")", 10);
incre ++;
}
setTimeout(function(){
customScrollToElement();
},100);
您可以通过首先使用 jQuery 获取元素相对于其父元素的位置来进行一些计算。
var left = -1 * ( $('#p4').position().left + 100 ),
top = -1 * ( $('#p4').position().top );
myScroller.scrollTo(left, top, 1000);
这将在元素右侧滚动 100 像素