2015-01-09 3 views
1

Я использую iScroll для горизонтальной прокрутки, и у меня длинная горизонтальная таблица, и я пытаюсь увидеть, видны ли определенные столбцы.Проверьте, отображается ли элемент на экране JavaScript/jQuery/iScroll

Я попытался следующий код, который я нашел в Интернете:

function checkVisible(elm, evalType) { 
evalType = evalType || "visible"; 

var vpH = $(window).height(), // Viewport Height 
    st = $(window).scrollTop(), // Scroll Top 
    y = $(elm).offset().top, 
    elementHeight = $(elm).height(); 

if (evalType === "visible") return ((y < (vpH + st)) && (y > (st - elementHeight))); 
if (evalType === "above") return ((y < (vpH + st))); 
alert('done'); 
}; 

однако, кажется, всегда говорят мне, что элемент отображается на экране, даже если это не так. Как бы я ни мог узнать, действительно ли этот столбец на экране отображается в браузере?

Спасибо.

+0

будут элементы скрыты (дисплей: нет), или они будут загружаться при прокрутке? –

+0

Пока, вся таблица (все элементы) предварительно загружена. – ittoryu

+0

Если это так, то они видны. Я думаю, вы хотите проверить, видны ли элементы в окне, не так ли? –

ответ

2

Верхняя граница видимой части экрана страницы будет $(window).scrollTop()

Нижняя граница будет $(window).scrollTop() + $(window).height()

(«верхний» и «нижний» имеют в виду физические границы)

Определите, находится ли $(element).offset().top между этими значениями, чтобы узнать, находится ли ваш элемент в видимом окне просмотра.

Я встроил это в обработчике прокрутки, но вы можете просто создать функцию, чтобы проверить свои элементы :)

Смотрите эту скрипку полного примера (смотреть консоль, когда зеленый ДИВ получает виден): http://jsfiddle.net/6v8j8z8e/3/

$(function() { 
 
    $(window).on("scroll", function() { 
 
     var up = $(window).scrollTop(), 
 
      down = $(window).scrollTop() + $(window).height(); 
 
     
 
     if(($("div").offset().top > up) && ($("div").offset().top < down)) { 
 
      console.log("green div is visible"); 
 
     } 
 
    }); 
 
});
div { 
 
    background: green; 
 
    color: white; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. <div>TARGET</div>Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolor quidem nesciunt consequatur perspiciatis deserunt quasi quas ex ullam obcaecati laudantium voluptatum eveniet tenetur animi debitis minima explicabo unde incidunt neque!

+0

Спасибо, это помогло мне заставить его работать ! Не могу подняться, пока у меня репутация, но я это ценю! – ittoryu