Выполнять функцию для загрузки только тогда, когда пользователь прокручивается в определенный div id [duplicate]

33
задан Aeonius 6 May 2011 в 13:09
поделиться

4 ответа

Плагин jQuery Waypoints http://imakewebthings.github.com/jquery-waypoints/ должен выполнить трюк

UPDATE

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery Waypoints Plugin - Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
    <script src="http://imakewebthings.github.com/jquery-waypoints/waypoints.min.js" type="text/javascript"></script>
    <style type="text/css">
        #mydiv {background-color:#FF0000; margin:1500px 0;}
    </style>
</head>
<body>

<div id="mydiv">
    Content goes here
</div>

<script type="text/javascript">
$(function() {
       $('#mydiv').waypoint(function() {
         window.location.href = 'http://google.com';
         }, {
           offset: '100%'
         });
    });
</script>
</body>
</html>
43
ответ дан Pav 25 August 2018 в 03:57
поделиться

jQuery Bullseye: http://static.pixeltango.com/jQuery/Bullseye/ также отлично справляется с обнаружением в окне просмотра!

6
ответ дан Benny Neugebauer 25 August 2018 в 03:57
поделиться

Возможно, вы захотите также увидеть следующий крошечный плагин, это поможет мне в прошлом, и это довольно чисто:

Пример использования:

$('div').bind('inview', monitor);
function monitor(event, visible)
{
    if(visible)
    {
      // element is now visible in the viewport
    }
    else
    {
      // element has gone out of the viewport
    }
}
18
ответ дан Chris Villa 25 August 2018 в 03:57
поделиться

https://github.com/stutrek/scrollMonitor

var scrollMonitor = require("./scrollMonitor"); // if you're not using require, you can use the scrollMonitor global.
var myElement = document.getElementById("itemToWatch");

var elementWatcher = scrollMonitor.create( myElement );

elementWatcher.enterViewport(function() {
    console.log( 'I have entered the viewport' );
});
elementWatcher.exitViewport(function() {
    console.log( 'I have left the viewport' );
});

elementWatcher.isInViewport - true if any part of the element is visible, false if not.
elementWatcher.isFullyInViewport - true if the entire element is visible [1].
elementWatcher.isAboveViewport - true if any part of the element is above the viewport.
elementWatcher.isBelowViewport - true if any part of the element is below the viewport.
4
ответ дан sakabako 25 August 2018 в 03:57
поделиться
Другие вопросы по тегам:

Похожие вопросы: