有很多很多圖片網站。 但是,用戶只需要立即收到前15張圖像。
document.addEventListener("DOMContentLoaded", function()
{
\t function loaded_3()
\t {
\t document.getElementById("left").innerHTML += "<br><br><strong>ALL DONE :)</strong><br><br>";
\t }
\t function loaded_2()
\t {
\t document.getElementById("left").innerHTML += "<br><br><strong>OK - images 16-30 have also loaded successfully!</strong><br>NOW LOADING THE REMAINING 5 IMAGES!<br>";
\t var last_divs = Array.prototype.slice.call(document.querySelectorAll('[data-last-id]'));
\t \t last_divs.forEach(function(element)
\t \t {
\t \t element.setAttribute('id', element.getAttribute('data-last-id'));
\t \t document.getElementById("left").innerHTML += 'id="' + element.getAttribute('data-last-id') + '" was set successfully!<br>';
\t \t });
\t window.removeEventListener("load", loaded_2);
\t window.addEventListener("load", loaded_3);
\t }
\t function loaded_1()
\t {
\t document.getElementById("left").innerHTML += "<strong>OK - images 1-15 have loaded successfully!</strong><br><br>";
\t var divs = Array.prototype.slice.call(document.querySelectorAll('[data-id]'));
\t \t divs.forEach(function(element)
\t \t {
\t \t element.setAttribute('id', element.getAttribute('data-id'));
\t \t document.getElementById("left").innerHTML += 'id="' + element.getAttribute('data-id') + '" was set successfully!<br>';
\t \t });
\t window.removeEventListener("load", loaded_1);
\t window.addEventListener("load", loaded_2);
\t }
\t window.addEventListener("load", loaded_1);
});
*
{
padding:0px;
margin:0px;
border-style:none;
box-sizing:border-box;
}
html, body
{
width:100%;
height:100%;
}
#left
{
position:absolute;
width:400px;
left:0px;
top:0px;
bottom:0px;
background-color:#ffcc00;
padding:20px;
}
#right
{
position:absolute;
left:400px;
top:0px;
right:0px;
bottom:0px;
background-color:#eeeeee;
padding:20px;
}
#one { background-image:url(https://placehold.it/999x999/ffcc00/cc33ff); }
#two { background-image:url(https://placehold.it/999x999/ff0000/000000); }
#three { background-image:url(https://placehold.it/999x999/00cc00/33cccc); }
#four { background-image:url(https://placehold.it/999x999/330033/ff33ff); }
#five { background-image:url(https://placehold.it/999x999/ffcccc/0033ff); }
#six { background-image:url(https://placehold.it/999x999/bbcc11/dddd00); }
#seven { background-image:url(https://placehold.it/999x999/0033bb/222bbb); }
#eight { background-image:url(https://placehold.it/999x999/2211bb/00bb11); }
#nine { background-image:url(https://placehold.it/999x999/ffcc00/22ccff); }
#ten { background-image:url(https://placehold.it/999x999/ffcc00/33ccff); }
#eleven { background-image:url(https://placehold.it/999x999/f0cc00/333333); }
#twelve { background-image:url(https://placehold.it/999x999/f1c20b/c13bf2); }
#thirteen { background-image:url(https://placehold.it/999x999/f0cd00/cf33f0); }
#fourteen { background-image:url(https://placehold.it/999x999/ff0b01/0001ff); }
#fifteen { background-image:url(https://placehold.it/999x999/ffbb00/dd31ff); }
#sixteen { background-image:url(https://placehold.it/999x999/ffd100/cc33ff); }
#seventeen { background-image:url(https://placehold.it/999x999/ffec10/000000); }
#eighteen { background-image:url(https://placehold.it/999x999/fbccfd/ff33ff); }
#nineteen { background-image:url(https://placehold.it/999x999/ccccff/cf33f0); }
#twenty { background-image:url(https://placehold.it/999x999/00ccee/cc33ff); }
#twentyone { background-image:url(https://placehold.it/999x999/ddccee/dddd00); }
#twentytwo { background-image:url(https://placehold.it/999x999/bbee00/cc33ff); }
#twentythree { background-image:url(https://placehold.it/999x999/11c000/c13bf2); }
#twentyfour { background-image:url(https://placehold.it/999x999/44c200/00bb11); }
#twentyfive { background-image:url(https://placehold.it/999x999/22ca00/33cccc); }
#twentysix { background-image:url(https://placehold.it/999x999/bbcf00/cc33ff); }
#twentyseven { background-image:url(https://placehold.it/999x999/ffbb00/00bb11); }
#twentyeight { background-image:url(https://placehold.it/999x999/eedd00/cc33ff); }
#twentynine { background-image:url(https://placehold.it/999x999/00ff00/33ccff); }
#thirty { background-image:url(https://placehold.it/999x999/220000/c13bf2); }
.testdiv
{
width:30px;
height:30px;
overflow:hidden;
margin:5px;
display:inline-block;
font-weight:bold;
font-size:30px;
color:#ffffff;
text-align:center;
line-height:30px;
}
<div id="left"></div>
<div id="right">
<div class="testdiv" id="one">1</div>
<div class="testdiv" id="two">2</div>
<div class="testdiv" id="three">3</div>
<div class="testdiv" id="four">4</div>
<div class="testdiv" id="five">5</div>
<div class="testdiv" id="six">6</div>
<div class="testdiv" id="seven">7</div>
<div class="testdiv" id="eight">8</div>
<div class="testdiv" id="nine">9</div>
<div class="testdiv" id="ten">10</div>
<div class="testdiv" id="eleven">11</div>
<div class="testdiv" id="twelve">12</div>
<div class="testdiv" id="thirteen">13</div>
<div class="testdiv" id="fourteen">14</div>
<div class="testdiv" id="fifteen">15</div>
<div class="testdiv" data-id="sixteen">16</div>
<div class="testdiv" data-id="seventeen">17</div>
<div class="testdiv" data-id="eighteen">18</div>
<div class="testdiv" data-id="nineteen">19</div>
<div class="testdiv" data-id="twenty">20</div>
<div class="testdiv" data-id="twentyone">21</div>
<div class="testdiv" data-id="twentytwo">22</div>
<div class="testdiv" data-id="twentythree">23</div>
<div class="testdiv" data-id="twentyfour">24</div>
<div class="testdiv" data-id="twentyfive">25</div>
<div class="testdiv" data-last-id="twentysix">26</div>
<div class="testdiv" data-last-id="twentyseven">27</div>
<div class="testdiv" data-last-id="twentyeight">28</div>
<div class="testdiv" data-last-id="twentynine">29</div>
<div class="testdiv" data-last-id="thirty">30</div>
</div> <!-- right -->
下一個10個圖像應儘快作爲第一圖像15裝入裝載。 最後5張圖片應該在第一張15張和下一張10張圖片完成加載後加載。
這裏是我的嘗試: https://filebin.net/01s7w1grpybvkiis/fourth.html
我的方法是設置一個id =「」只是,讓CSS做媒體的查詢。 當然,我可以使用JS加載圖像(然後我可以很容易地找出哪些已加載),但我需要照顧兩者,CSS寬度和也像素比!
所以我不能加載JS中的特定圖像,因爲要加載的背景圖像將是不同的,這取決於CSS寬度和像素比率(未在我的測試案例中實現,但您需要具有此信息!)。
這就是爲什麼我與JS和CSS嘗試了一起(參見例如:https://filebin.net/01s7w1grpybvkiis/fourth.html)
我發現了一個庫JS(名爲enquire.js),它說,它可以做媒體查詢純JS,但未能使用媒體查詢的像素比,所以我再次嘗試CSS媒體查詢。
我該如何爲此編寫純JavaScript解決方案?我不喜歡在這個階段使用jQuery。
嗨,你能不能請在這裏編輯你的代碼的相關位進入正題?即使任何鏈接的外部資源出現故障,堆棧溢出的問題預計也會有效。 –
你也可以澄清你想達到什麼 - 這個'下一個10張圖片應該在第一個15張圖片加載後立即加載'聽起來像是在將一張圖片一個接一個地列出時的默認行爲,不是? –
@皮卡웃我看了一下,代碼非常廣泛,所以我沒有在這裏複製它。 Ronny,嘗試在編輯器中使用'<>'按鈕在這裏創建一個[mcve] – mplungjan