javascript - How to update a div based on it's position relative to another div -


i trying find best way update div id "date" dates of divs scrolled past. thought may done through getting data-date attributes having trouble obtaining ids of divs scrolled past. in case content of date div, should start out 25 july , div id=2 passes date div, content of date div should change 25 july , on.

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> 

i've added class date divs, such

<div class="mydates" id="1" data-date="25 july">

then

$(document).ready(function() {            function viewportcheck(el) {              function iselementinviewport(el) {                  var rect = el.getboundingclientrect();                  return (                      rect.top >= 0 &&                      rect.left >= 0 &&                      rect.bottom <= (window.innerheight || document.documentelement.clientheight) &&                      rect.right <= (window.innerwidth || document.documentelement.clientwidth)                  );              }                function givenelementinviewport(el) {                  return function() {                      if (iselementinviewport(el)) {                          let d = el.dataset.date;                          $('#date').html(d);                      }                  };              }                if (window.addeventlistener) {                  addeventlistener('domcontentloaded', givenelementinviewport(el), false);                  addeventlistener('load', givenelementinviewport(el), false);                  addeventlistener('scroll', givenelementinviewport(el), false);                  addeventlistener('resize', givenelementinviewport(el), false);              } else if (window.attachevent) {                  attachevent('domcontentloaded', givenelementinviewport(el));                  attachevent('load', givenelementinviewport(el));                  attachevent('scroll', givenelementinviewport(el));                  attachevent('resize', givenelementinviewport(el));              }          }            $(function () {              $(window).scroll(function (event) {                  let elems = $('.mydates');                  for(let = 0; < elems.length; i++ ){                      viewportcheck(elems[i]);                  }              });          });      });
    #comments {          float:left;          width:450px;      }        #date {          position: absolute;          top: 10px;          right:10px;          border: 1px solid #a9f5f2;          background-color: #a9f5f2;          color: #6e6e6e;          font-weight: bold;          min-width:50px;      }        .mydates {          min-height: 30%;          margin: 10% 10px;      }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="date" style="float:right; position: fixed">      &nbsp;  </div>    <div class="mydates" 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 class="mydates" 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 class="mydates" 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 class="mydates" 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 class="mydates" 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>


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -