요소의 스크롤 비율을 시점에 입력하자마자 계산하는 방법은 무엇입니까?jQuery - 요소가 벡터로 들어갈 때 요소의 스크롤 비율을 계산 하시겠습니까?
는 은 소스의 몇 가지의 코드 내 조합:
$(document).ready(function(){
var initY = $('.scrollratio').offset().top
var height = $('.scrollratio').height()
var endY = initY + $('.scrollratio').height()
$(window).scroll(function(){
var scroll = $(window).scrollTop()
var visible = isVisible(document.getElementById("demo"))
console.log(visible)
if (visible) {
console.log('in view')
} else {
console.log('out of view')
}
if(visible){
var diff = scroll - initY
var ratio = Math.round((diff/height) * 100)
$('#note').text(ratio)
}
})
})
// Check if the element is in the viewport.
// http://www.hnldesign.nl/work/code/check-if-element-is-visible/
function isVisible(node) {
// Am I visible?
// Height and Width are not explicitly necessary in visibility detection, the bottom, right, top and left are the
// essential checks. If an image is 0x0, it is technically not visible, so it should not be marked as such.
// That is why either width or height have to be > 0.
var rect = node.getBoundingClientRect()
return (
(rect.height > 0 || rect.width > 0) &&
rect.bottom >= 0 &&
rect.right >= 0 &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.left <= (window.innerWidth || document.documentElement.clientWidth)
)
}
* {
padding: 0;
margin: 0;
}
body, html {
height: 100%;
margin: 0;
font: 400 15px/1.8 "Lato", sans-serif;
color: #777;
}
body {
background:#171717;
}
.section-1 {
width: 100%;
min-height: 100%;
}
.scrollratio {
height: 2000px;
background:red;
}
#note {
position: fixed;
top: 0;
left: 0;
color: #FFFFFF;
margin: 2em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="note" ></div>
<div class="section-1"></div>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<div class="scrollratio" id="demo"></div>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
요소 (빨간색 상자) 뷰포트를 입력하면 그것은 음의 값을 가져오고 은 가져 요소의 상단이 윈도우 상단을 통과하기 시작할 때 양수 값.
어떤 아이디어가 뷰포트에있는 즉시의 양수 값 을 얻는 방법?
편집 :
$(document).ready(function(){
var initY = $('.scrollratio').offset().top
var height = $('.scrollratio').height()
var endY = initY + $('.scrollratio').height()
var wHeight = $(window).height();
$(window).scroll(function(){
var scroll = $(window).scrollTop()
var visible = isVisible(document.getElementById("demo"))
if(visible){
var diff = scroll + wHeight - initY
var ratio = Math.round((diff/height) * 100)
$('#note').text(ratio)
}
})
})
// Check if the element is in the viewport.
// http://www.hnldesign.nl/work/code/check-if-element-is-visible/
function isVisible(node) {
// Am I visible?
// Height and Width are not explicitly necessary in visibility detection, the bottom, right, top and left are the
// essential checks. If an image is 0x0, it is technically not visible, so it should not be marked as such.
// That is why either width or height have to be > 0.
var rect = node.getBoundingClientRect()
return (
(rect.height > 0 || rect.width > 0) &&
rect.bottom >= 0 &&
rect.right >= 0 &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.left <= (window.innerWidth || document.documentElement.clientWidth)
)
}
* {
padding: 0;
margin: 0;
}
body, html {
height: 100%;
margin: 0;
font: 400 15px/1.8 "Lato", sans-serif;
color: #777;
}
body {
background:#171717;
}
.section-1 {
width: 100%;
min-height: 100%;
}
.scrollratio {
height: 2000px;
background:red;
}
#note {
position: fixed;
top: 0;
left: 0;
color: #FFFFFF;
margin: 2em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="note" ></div>
<div class="scrollratio" id="demo"></div>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br>
감사합니다. – laukok
빨간 상자가 맨 위에있을 때 (section-1없이), 10 번이나 40 번부터 시작됩니다. 위의 편집을 참조하십시오. – laukok
글쎄 그것은이 비율에 대한 당신의 목표에 달려 있습니다. 창 높이를 포함하면 div가 화면 아래쪽에서 화면으로 들어갈 때 (스크롤 할 때) 비율이 0이어야합니다. 'section-1'이 제거되면, div는 이미 화면의 상단에 있으므로, 비율은> 0입니다. 그러나 양쪽 모두를 "수용"하는 한 가지 방법은'wHeight' 조건부를 만드는 것입니다 - 업데이트 된 답변보기 –