Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery窗口滚动函数到一个id,然后做一些事情_Jquery - Fatal编程技术网

jquery窗口滚动函数到一个id,然后做一些事情

jquery窗口滚动函数到一个id,然后做一些事情,jquery,Jquery,我只是想知道,当使用窗口滚动功能而不是像素高度时,是否可能触发警报。我问这个问题的原因是,我意识到高度会根据显示位置的不同而变化,因此引用id可能更有效。通过使用,可以获得元素的位置。 结合你的滚动功能,你将有一个“窗口滚动功能到一个id” 使用可以获得元素的位置。 结合你的滚动功能,你将有一个“窗口滚动功能到一个id” 使用jQuery可以将页面设置为id // store the position of the element in position var position = $('#i

我只是想知道,当使用窗口滚动功能而不是像素高度时,是否可能触发警报。我问这个问题的原因是,我意识到高度会根据显示位置的不同而变化,因此引用id可能更有效。

通过使用,可以获得元素的位置。 结合你的滚动功能,你将有一个“窗口滚动功能到一个id”

使用可以获得元素的位置。 结合你的滚动功能,你将有一个“窗口滚动功能到一个id”


使用jQuery可以将页面设置为id

// store the position of the element in position
var position = $('#idOfElement').offset(); //=position() but always relative to 
                                           //document instead of parent. 
                                           //thanks to sidonaldson

// on scrolling of the document do something
$(document).scroll(function () {
    //the current height
    var y = $(this).scrollTop();

    //If the current Y is bigger than the element. (you scrolled beyond the element)
    if(y >= position.top){
        //do something
    }else{
        //do something else 
    }
});
$("html, body").animate({ scrollTop: $('#your-id').offset().top }, 1000, function(){
    alert("Scroll end");
});
首先,您必须使用“html,body”使其跨浏览器

然后,通过计算id元素相对于页面顶部的偏移量(相对于相对于父元素的位置),计算出id元素的位置

然后以毫秒为单位设置速度

最后,您传入一个函数引用,或者在本例中,定义一个回调函数——您可以在这里添加警报


请注意,这必须在document.ready被激发之后进行。

使用jQuery可以将页面动画化为id

// store the position of the element in position
var position = $('#idOfElement').offset(); //=position() but always relative to 
                                           //document instead of parent. 
                                           //thanks to sidonaldson

// on scrolling of the document do something
$(document).scroll(function () {
    //the current height
    var y = $(this).scrollTop();

    //If the current Y is bigger than the element. (you scrolled beyond the element)
    if(y >= position.top){
        //do something
    }else{
        //do something else 
    }
});
$("html, body").animate({ scrollTop: $('#your-id').offset().top }, 1000, function(){
    alert("Scroll end");
});
首先,您必须使用“html,body”使其跨浏览器

然后,通过计算id元素相对于页面顶部的偏移量(相对于相对于父元素的位置),计算出id元素的位置

然后以毫秒为单位设置速度

最后,您传入一个函数引用,或者在本例中,定义一个回调函数——您可以在这里添加警报

请注意,此操作必须在document.ready启动后进行。

尝试以下操作:

//getting the Y position of the element you want to scroll to
var position = $('#element').offset().top;

//scrolling to the element with an animation of 700 miliseconds
$('body').animate({
    scrollTop: position
}, 700, function(){  //callback function (executed when animation finishes)
    alert("Hello there!");
});
生活演示:

试试这个:

//getting the Y position of the element you want to scroll to
var position = $('#element').offset().top;

//scrolling to the element with an animation of 700 miliseconds
$('body').animate({
    scrollTop: position
}, 700, function(){  //callback function (executed when animation finishes)
    alert("Hello there!");
});

现场演示:

他不想滚动到某个
el.top
;但是当他到达
el.top
时想要触发一些东西。至少我是这样理解他的问题的。他不想滚动到某个
el.top
;但是当他到达
el.top
时想要触发一些东西。至少这是我对他的问题的理解。据我所知,你不需要使用
html,body
。只要使用
body
,它就可以适用于我测试过的所有浏览器。(IE9、Chrome、Opera 12、Firefox……)哪些没有?这个问题告诉你为什么:)不是真的。他没有指定浏览器,也没有提供任何数据链接来证明这一点。他可能指的是非常古老的浏览器…阿尔瓦罗-这是一种公认的跨浏览器滚动页面的方式。之前版本中的Safari存在问题,浏览器处理“设置”和“获取”scrollTop的方式也存在差异。我无法确切说明原因,因为这是一种长期以来被接受的做法。是的,这很容易解决,我只是对它有点好奇,因为到目前为止,我没有注意到任何浏览器中有任何问题。无论如何谢谢你!据我所知,您不需要使用
html,body
。只要使用
body
,它就可以适用于我测试过的所有浏览器。(IE9、Chrome、Opera 12、Firefox……)哪些没有?这个问题告诉你为什么:)不是真的。他没有指定浏览器,也没有提供任何数据链接来证明这一点。他可能指的是非常古老的浏览器…阿尔瓦罗-这是一种公认的跨浏览器滚动页面的方式。之前版本中的Safari存在问题,浏览器处理“设置”和“获取”scrollTop的方式也存在差异。我无法确切说明原因,因为这是一种长期以来被接受的做法。是的,这很容易解决,我只是对它有点好奇,因为到目前为止,我没有注意到任何浏览器中有任何问题。无论如何谢谢你!谢谢,这正是我一直在寻找的:)使用偏移量而不是位置。下次请把你的问题说得更好,这样人们就不会浪费时间回答了!谢谢,这正是我一直在寻找的:)使用偏移量而不是位置。下次请把你的问题说得更好,这样人们就不会浪费时间回答了!