2017-05-07 52 views
0

なぜ機能していないのか理解しやすくすることができますか?jQueryが動的に追加された子要素の合計高さdivを計算しない

DIV要素に動的に追加/配置されるすべての子要素の合計高さを計算しようとしていますが、コードは機能していません。子要素の数は毎回変わります]。このコードは、console.log()で返された値をチェックしたときに最初に定義されたゼロの値を返しています。ここで

は、コードと印刷scrreenです:

//This is the code that dynamically extracts data every second from the server and populates it into to DIV element. 
      setInterval(function() { 
       google.script.run.withSuccessHandler(function(data) { 
        var temp3 = ''; 
        for (var i = 0; i < data.length; i++) { 
         temp3 += '<p>' + data[i].join() + '</p>'; 
        } 
        document.getElementById("item1").innerHTML = temp3; 
       }).getReadyLine(); 
      }, 1000); 

//This is the code that should calculate the total height of all the children elements of DIV element. But it is not working, not calculating, it displays 0 on the console.log(). 
      var outerHeight1 = 0; 
      $("#item1").children().each(function() { 
       outerHeight1 += $(this).outerHeight(); 
      }); 
     </script> 
//This is the code for DIV. 
     <div class="item1" id="item1"> 

Print Screen

PS

Update: included the code inside setInerval like this: 
<script> 

    $(document).ready(function() { 
     setInterval(function() { 
      google.script.run.withSuccessHandler(function(data) { 
       var temp3 = ''; 
       for (var i = 0; i < data.length; i++) { 
        temp3 += '<p>' + data[i] + '</p>'; 
       } 
       document.getElementById("item1").innerHTML = temp3; 
      }).getReadyLine(); 
      var outerHeight1 = 0; 
       $("#item1").children().each(function() { 
        outerHeight1 += $(this).outerHeight(true); 
        return outerHeight1; 
       }); 
       console.log(outerHeight1); 
     }, 1000); 

     /* 
     var outerHeight1 = 0; 
     $("#item1").children().each(function() { 
      outerHeight1 += $(this).outerHeight(); 
     }); */ 

     var outerHeight2 = 0; 
     $("#item2").children().each(function() { 
      outerHeight2 += $(this).outerHeight(true); 
     }) 

     $("#item1").animate({ 
      scrollTop: outerHeight1 
     }, 10000); 
     setTimeout(function() { 
      $("#item1").animate({ 
       scrollTop: 0 
      }, 10000); 
     }, 3000); 
     setInterval(function() { 
      // 4000 - it will take 4 secound in total from the top of the page to the bottom 
      $("#item1").animate({ 
       scrollTop: outerHeight1 
      }, 10000); 
      setTimeout(function() { 
       $("#item1").animate({ 
        scrollTop: 0 
       }, 10000); 
      }, 4000); 

     }, 4000); 

     $("#item2").animate({ 
      scrollTop: outerHeight2 
     }, 8000); 
     setTimeout(function() { 
      $("#item2").animate({ 
       scrollTop: 0 
      }, 8000); 
     }, 8000); 
     setInterval(function() { 
      // 4000 - it will take 4 secound in total from the top of the page to the bottom 
      $("#item2").animate({ 
       scrollTop: outerHeight2 
      }, 8000); 
      setTimeout(function() { 
       $("#item2").animate({ 
        scrollTop: 0 
       }, 8000); 
      }, 8000); 

     }, 8000); 

     console.log($("#item1").outerHeight()); 
     console.log(outerHeight1); 
     }); 




    </script> 
+0

あなたがここにフィドルやコードスニペットを投稿することができた場合は、それが参考になります。 – gaganshera

+1

問題は、hightを一度だけ計算していて、 '#item1'に要素が追加される前に完了しているということです。 – Titus

+0

正確には、あなたは' setInterval'の外でそれをやっています – charlietfl

答えて

0

は、1時間の計算上のヒントをありがとうとのsetInterval部分の内側にコードを埋め込みます。ここで

は動作するコードです:

$(document).ready(function() { 
      $("#item1").animate({ 
       scrollTop: $("#item1").height() 
      }, 4000); 

      setTimeout(function() { 
       $("#item1").animate({ 
        scrollTop: 0 
       }, 4000); 
      }, 4000); 

      setInterval(function() { 
       // 4000 - it will take 4 secound in total from the top of the page to the bottom 
       $("#item1").animate({ 
        scrollTop: $("#item1").height() 
       }, 4000); 
       setTimeout(function() { 
        $("#item1").animate({ 
         scrollTop: 0 
        }, 4000); 
       }, 4000); 

      }, 8000); 
     }); 

     var totalHeight = 0; 

     $("#leftcolumn").children().each(function() { 
      totalHeight = totalHeight + $(this).outerHeight(true); 
     }); 

     var outerHeight = 0; 
     $('.profile').each(function() { 
      outerHeight += $(this).outerHeight(); 
     }); 
     $("#total-height").text(outerHeight + 'px'); 

     setInterval(function() { 
      google.script.run.withSuccesshandler(function(data) { 
       $("input[type=text]").each(function() { 
        out += "<li>" + $(this).val() + "</li>"; 
       }); 
       out += "</ul>"; 

       $('#listView').html(out); 
      }).getReadyLine(); 
     }, 50000); 

     $('#mybutt').click(function() { 
      var out = '<ul>'; 
      $("input[type=text]").each(function() { 
       out += "<li>" + $(this).val() + "</li>"; 
      }); 
      out += "</ul>"; 

      $('#listView').html(out); 
     }); 
     setInterval(function() { 
      google.script.run.withSuccessHandler(function(data) { 
       console.log(data); 
       document.getElementById("div").innerHTML = whatToWrite; 
      }).getReadyLine(); 
     }, 1000); 

     setInterval(function() { 
      google.script.run.withSuccessHandler(function(data) { 
       console.log(data); 
       var temp3 = ''; 
       for (var i = 0; i < data.length; i++) { 
        temp3 = 'p' + i + '/p'; 
        document.getElementById("#item1").innerHTML = temp3; 
       } 
      }).getReadyLine(); 
     }, 1000); 


     setInterval(function() { 
      google.script.run.withSuccessHandler(function(data) { 
       var temp3 = ''; 
       for (var i = 0; i < data.length; i++) { 
        temp3 = 'p' + data[i] + '/p'; 
        document.getElementById("#item1").innerHTML = temp3; 
        temp3 = ''; 
       } 
      }).getReadyLine(); 
     }, 1000); 

     //This is the server side code that returns the data, it works fine, have checked it; 
     function getReadyLine() { 
      var rawData = sheetMAT.getRange(3, 2, sheetMAT.getLastRow() - 2, 5).getValues(); 
      for (var i = 0; i < rawData.length; i++) { 
       if (rawData[i][3] === "A Ready Line" && rawData[i][4] === "ATY") { 
        temp1 = ' ' + data[i][0]; 
        temp2.push(data[i][1], temp1); 
        dataReadyLine.push(temp2); 
        temp1 = ''; 
        temp2 = []; 
       } 
      } 
      return dataReadyLine; 
     } 

     //This is the client side code that should populate the innerHTML of the DIV template that is shown below; 
     setInterval(function() { 
      google.script.run.withSuccessHandler(function(data) { 
       var temp3 = ''; 
       for (var i = 0; i < data.length; i++) { 
        temp3 = 'p' + data[i] + '/p'; 
        document.getElementById("#item1").innerHTML = temp3; 
        temp3 = ''; 
       } 
      }).getReadyLine(); 
     }, 10000); 

     setInterval(function() { 
      google.script.run.withSuccessHandler(function(data) { 
       var temp3 = ''; 
       for (var i = 0; i < data.length; i++) { 
        temp3 += '<p>' + data[i].join() + '</p>'; 
       } 
       document.getElementById("item1").innerHTML = temp3; 
      }).getReadyLine(); 
     }, 1000); 

     //This is the code that dynamically extracts data every second from the server and populates it into to DIV element. 
     setInterval(function() { 
      google.script.run.withSuccessHandler(function(data) { 
       var temp3 = ''; 
       for (var i = 0; i < data.length; i++) { 
        temp3 += '<p>' + data[i].join() + '</p>'; 
       } 
       document.getElementById("item1").innerHTML = temp3; 
      }).getReadyLine(); 
     }, 1000); 

     //This is the code that should calculate the total height of all the children elements of DIV element. But it is not working, not calculating. 
     var outerHeight1 = 0; 
     $("#item1").children().each(function() { 
      outerHeight1 += $(this).outerHeight(); 
     }); 


     $(document).ready(function() { 
      setInterval(function() { 
       google.script.run.withSuccessHandler(function(data) { 
        var temp3 = ''; 
        for (var i = 0; i < data.length; i++) { 
         temp3 += '<p>' + data[i] + '</p>'; 
        } 
        document.getElementById("item1").innerHTML = temp3; 
       }).getReadyLine(); 
       var outerHeight1 = 0; 
       $("#item1").children().each(function() { 
        outerHeight1 += $(this).outerHeight(true); 
       }); 
       console.log(outerHeight1); 
       $("#item1").animate({ 
        scrollTop: outerHeight1 
       }, 8000); 
       setTimeout(function() { 
        $("#item1").animate({ 
         scrollTop: 0 
        }, 8000); 
       }, 1000); 
       setInterval(function() { 
        // 4000 - it will take 4 secound in total from the top of the page to the bottom 
        $("#item1").animate({ 
         scrollTop: outerHeight1 
        }, 8000); 
        setTimeout(function() { 
         $("#item1").animate({ 
          scrollTop: 0 
         }, 8000); 
        }, 8000); 

       }, 1000); 
      }, 1000); 

      var outerHeight2 = 0; 
      $("#item2").children().each(function() { 
       outerHeight2 += $(this).outerHeight(true); 
      }) 

      $("#item2").animate({ 
       scrollTop: outerHeight2 
      }, 8000); 
      setTimeout(function() { 
       $("#item2").animate({ 
        scrollTop: 0 
       }, 8000); 
      }, 8000); 
      setInterval(function() { 
       // 4000 - it will take 4 secound in total from the top of the page to the bottom 
       $("#item2").animate({ 
        scrollTop: outerHeight2 
       }, 8000); 
       setTimeout(function() { 
        $("#item2").animate({ 
         scrollTop: 0 
        }, 8000); 
       }, 8000); 

      }, 8000); 

     }); 
+0

あなたはこのようにしなければなりません。 jQueryの 'animate'関数は、アニメーションのさまざまなステージ(アニメーションが開始されたとき、完了したときなど)に多くのコールバック関数を持っています。その詳細については、http://api.jquery .com/animate /)。例えば、 'setTimeout'を使う代わりに、' $( "#item1")。amimate({scrollTop:$( "#item1")} height()}、4000、function(){$ this ).animate({scrollTop:0}、4000)})) ' – Titus

+0

高さが固定された「#item1」があります。その隣にボックスがあります。また、** setTimeout **を使わずに繰り返しスクロールダウンすることもできませんでした。 – Din

関連する問題