无法对文本应用延迟加载

时间:2017-11-14 11:10:41

标签: javascript html css

我正在开发一个简单的用户界面,我想在其中使用' vanilla'在文本中添加 lazy-loading 概念。 JavaScript的。我现在正在显示有关10个用户的信息。当我要向下滚动时,我想加载其余的信息。为此,我找出了当前的滚动位置。如果它到达底部,则要将下一个数据附加到现有数据。我试图附加简单的文字,它的工作原理。但它无法从div中追加元素。我该如何纠正那段代码?感谢.. !!



var persons = [
    {firstname : "Malcom", lastname: "Reynolds", id:1},
    {firstname : "Kaylee", lastname: "Frye", id:2},
    {firstname : "Jayne", lastname: "Cobb", id:3},
    {firstname : "Mal", lastname: "Rlds", id:4},
    {firstname : "ylee", lastname: "ye", id:5},
    {firstname : "yne", lastname: "Cb", id:6},
    {firstname : "Mal", lastname: "Rolds", id:7},
    {firstname : "lee", lastname: "Fryedwwwwd", id:8},
    {firstname : "Jay", lastname: "bdwe", id:9},
    {firstname : "Maljh", lastname: "Rolds", id:10},
    {firstname : "ljyyu", lastname: "jhjFryed", id:11},
    {firstname : "ryJay", lastname: "jkmCdwe", id:12},
    {firstname : "yuryMal", lastname: "jhkhRolds", id:13},
    {firstname : "rtulee", lastname: "Frwwd", id:14},
    {firstname : "ryuyJay", lastname: "Cryyobbdwe", id:15}
];

var start = 0,size = 10, current = 0;

function getFullName(item, index) {
  return "<li>" + item.firstname + "<span>" + item.lastname + "</span> " + item.id + "</li>";
}

var getFullNameForHighlighting = function(searchText) {
    return function(item, index) {
       var index = item.firstname.toUpperCase().indexOf(searchText.toUpperCase())+searchText.length;
           return "<li>" + "<span class='highlight'>"+searchText + "</span>" +item.firstname.substring(index) + "<span>" + item.lastname + "</span> " + item.id + "</li>";
    }
};

function myFunction1(){
  current = size;
  document.getElementById("myUL").innerHTML = persons.map(getFullName).slice(start, current).join('');
  //start= current;
}

function myFunction() {
  current = size;
  var input, filter, ul, li, a, i, count = 0;
  input = document.getElementById("myInput");
  filter = input.value.toUpperCase();

  var longWords = persons.filter(function(person) {

    return person.firstname.toUpperCase().indexOf(filter) > -1
  });
  var message = "Result:" + longWords.length + " words containing '" + input.value;
  document.getElementById("demo").innerHTML = message;

  ul = document.getElementById("myUL");
  varInnerHtmlText = longWords.map(getFullNameForHighlighting(filter));
  ul.innerHTML = varInnerHtmlText;
}

var x = 0;
function myFunction3(e) {
  document.getElementById("demo").innerHTML = ++x; 
}
&#13;
* {
  box-sizing: border-box;
}

#myInput {
  width: 50%;
  font-size: 14px;
  padding: 8px 10px 8px 5px;
  border: 1px solid #ddd;
  margin-bottom: 12px;
  margin-top: 20px;
}

#clear {
  margin-left: 20px;
  margin-top: 40px;
  font-size: 14px;
  margin-right: 50px;
  padding: 4px 4px 4px 4px;
}

#myUL {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

#myUL li a {
  border: 1px solid #ddd;
  margin-top: -1px;
  /* Prevent double borders */
  background-color: #f6f6f6;
  padding: 8px;
  text-decoration: none;
  font-size: 16px;
  color: black;
  display: block;
  width: 100%;
}

#myUL li a:hover:not(.header) {
  background-color: #eee;
}

body {
  margin: 0;
}

.column {
  float: left;
  margin-top: 50px;
  padding: 10px;
  height: 550px;
}

.left {
  width: 20%;
}

.right {
  width: 60%;
}

.row:after {
  content: "";
  display: table;
  clear: both;
}

ul.demo {
  display: inline-block;
  border-bottom: 1px dotted black;
}

ul.demo li {
  position: right;
}

li:hover span {
  width: 100px;
  background-color: white;
  color: #fff;
  text-align: center;
  border-radius: 2px;
  position: absolute;
  z-index: 1;
  color: black;
}
/*li span { display: none; }*/
.highlight {
  font-weight: bold;
  color: black;
}

li em {
  background: #ff6;
  font-weight: bold;
  font-style: normal;
}
&#13;
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="two.css" />
<script src="two.js"></script>

<body onload="myFunction1()">
  <br>
  <br>

  <div class="row">
    <div class="column left" style="background-color:White;"></div>

    <div class="column right" style="background-color:#bbb;">
      <form id="myForm" onsubmit='return onSubmit(this)'>

        Find:
        <input type="text" class="demo1" id="myInput" align="center" onkeyup="myFunction()" placeholder="Search here...">

        <button id="clear" onclick="document.getElementById('myInput').value = ''"> Clear </button>

      </form>
      <br>
      <ul id="demo">
        15 words total
      </ul>
      <br>
      <div id ="myDIV" onscroll="myFunction3(this)" style="width:100%;height:250px;overflow:scroll;padding:5px;color:#714D03;scrollbar-base-color:#DEBB07;">

        <ul id="myUL" class="demo">

        </ul>
      </div>
       <p><span id="demo"></span></p> 
    </div>
  </div>
 

<script>
document.getElementById("myDIV").onwheel = function() {lazyLoad1()};

function lazyLoad1() {
    document.getElementById("myDIV").style.fontSize = "25px";
    var element = document.getElementById("myDIV");
    var a = element.scrollTop;
    var b = element.scrollHeight - element.clientHeight;
    var c = a / b;
    console.log(c);
    var scrollPos = window.scrollY || window.scollTop || document.getElementsByTagName("html")[0].scrollTop;
    console.log(scrollPos);     
    if(c===1)
    {
       current = 5;
       //document.getElementById("myUL").innerHTML = persons.map(getFullName).slice(start, current).join('');
   var newDiv = document.createElement("div"); 
   var newContent = document.createTextNode("Hi there and greetings!"); 
   newDiv.appendChild(newContent); //add the text node to the newly created div. 
 //  var currentDiv = document.getElementById("myDIV"); 
   //document.body.insertBefore(newDiv,currentDiv); 
   document.body.appendChild(newDiv);
    }
}
</script>
&#13;
&#13;
&#13;

0 个答案:

没有答案