Я пытаюсь найти лучший способ обновить div с идентификатором «дата» с датами прокрученных проходов. Я думал, что это можно сделать, получив атрибуты date-date, но у меня даже возникли проблемы с получением идентификаторов divs, которые прокручиваются мимо. Таким образом, в этом случае содержание даты div должно начинаться с 25 июля, а затем, когда div с id = 2 передает дату div, содержимое даты div должно измениться до 25 июля и так далее.Как обновить div на основе его позиции относительно другого div
index.html:
<html>
<head>
<style>
#comments {
float:left;
width:450px;
}
#date {
position: absolute;
top: 0;
margin-top: 20px;
border: 1px solid #A9F5F2;
background-color: #A9F5F2;
color: #6E6E6E;
font-weight: bold;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js">
</script>
<script>
$(document).ready(function() {
$(function() {
$(window).scroll(function (event) {
var p = $('#date').closest('[id]');
console.log(p);
});
});
});
</script>
</head>
<body>
<div id="1" data-date="25 July">
comment 1 comment 1 comment 1 comment 1 comment 1 comment 1 <p>
comment 1 comment 1 comment 1 <p>
comment 1 comment 1 comment 1 <p>
</div>
<div id="2" data-date="24 July">
comment 2 comment 2 comment 2 comment 2 comment 2 comment 2 <p>
comment 2 comment 2 comment 2 <p>
comment 2 comment 2 comment 2 <p>
</div>
<div id="3" data-date="23 July">
comment 3 comment 3 comment 3
comment 3 comment 3 comment 3 <p>
comment 3 comment 3 comment 3 <p>
comment 3 comment 3 comment 3 <p>
</div>
<div id="4" data-date="22 July">comment 4 comment 4 comment 4
comment 4 comment 4 comment 4 <p>
comment 4 comment 4 comment 4 <p>
comment 4 comment 4 comment 4
</div>
<div id="5" data-date="21 July">
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
comment 5 comment 5 comment 5 <p>
</div>
<div id="date" style="float:right; position: fixed">
25 July
</div>
</body>
</html>
Как вы обработка прокрутки? Это по умолчанию прокрутка браузера или какая-то библиотека? – Hodrobond
Просто fyi, .closest вернет ближайший элемент, основанный на dom, а не на положение экрана. Помните, что DOM - это то, с чем будут взаимодействовать любые javascript и html, несмотря на то, что на экране. –
В качестве альтернативы дате и дате даты, вы можете написать свои даты как id = '7-5-2015'. Не совсем уверен, что вы делаете, но я бы обрезал атрибут data и идентифицировал их все с датой. –