2017-09-23 80 views
0

我正在嘗試構建時間線來顯示一天中的事件時間表。該代碼位於https://jsfiddle.net/2r99xu02/2/。每當你點擊發言人名稱時,它會打開一個模式,其中包含有關與發言者有關的照片的一些信息。在沒有延遲的情況下以動態模式動態設置圖像

不過,我這裏面臨的問題是,img標籤的src屬性設置爲顯示和它當我打開模式,圖像被後來着手第二模態的時間,這不很好看。有什麼我可以做的,以確保只有當設置src屬性時,模態纔會顯示。

我使用jQuery和w3css

感謝

回答

1

使用jQuery中的load函數。

注:https://blog.jquery.com/2016/06/09/jquery-3-0-final-released/

刪除過時事件別名

.load,.unload和.error,因爲jQuery的1.8過時,沒有更多的。 使用.on()註冊偵聽器。

你說你用jQuery。然後使用它! 我已經更新了您的代碼(JS),並在發言者圖像(HTML)後添加了範圍

請檢查您的html結構和命名。按索引訪問元素是不好的。 給類的div的,跨度等


演示是不工作的,因爲圖像是不可用的。請 自己檢查一下。這只是一個例子,你可以做到這一點。

const info = { 
 
    'David Anderson': ' David Anderson Lorem ipsum dolor sit amet', 
 
    'John Doe': 'John Doe Lorem ipsum dolor sit amet', 
 
    'Mark Smith': 'Mark Smith Lorem ipsum dolor sit amet', 
 
    'Michael Lee': 'Michael Lee Lorem ipsum dolor sit amet', 
 
    'Steve Newman': 'Steve Newman Lorem ipsum dolor sit amet' 
 
}; 
 

 
function openSpeakerBio() { 
 
    const $name = $(this); 
 

 
    const name = $name.html().replace(/<br\s*\/?>/gi, ' '); 
 

 
    const imgName = './' + $name.text() + '.png'; 
 

 
    const $modal = $('#id02'); 
 
    const $header = $modal.find('h4'); 
 
    const $image = $modal.find('.modal-image'); 
 
    const $bio = $modal.find('.bio'); 
 
    const $fusionLogo = $('.fusion-logo-1x'); 
 

 
    $header.html(name); 
 
    $bio.find('span').html(info[name]); 
 
    $image.attr('src', imgName); 
 

 
    $image.on('load', function() { 
 
     $modal.show(); 
 
    }); 
 

 
    $(window).on('load', function() { 
 
     $fusionLogo.attr('src', '//new.network-data-cabling.co.uk/wp-content/uploads/2016/08/ACCL_Logo.svg'); 
 
    }); 
 
} 
 

 
function closeModal() { 
 
    const $modal = $('#id02'); 
 

 
    $modal.hide(); 
 
} 
 

 
$(function() { 
 
    // listen for events 
 
    $('.speaker img').on('click', openSpeakerBio); 
 
    $('.speaker h3').on('click', openSpeakerBio); 
 
    $('.close-modal').on('click', closeModal); 
 
});
/* Import */ 
 

 
@import url(./assets/timeline-fonts.css); 
 

 
/* Variables */ 
 

 

 
/* Base */ 
 

 
strong { 
 
    font-weight: 600; 
 
} 
 

 
.vertical-alignment-helper { 
 
    display: table; 
 
    height: 100%; 
 
    width: 100%; 
 
} 
 

 
.vertical-align-center { 
 
    /* To center vertically */ 
 
    display: table-cell; 
 
    vertical-align: middle; 
 
} 
 

 
.bio { 
 
    text-align: justify; 
 
} 
 

 

 
/* Timeline */ 
 

 
.timeline { 
 
    border-left: 4px solid #a5a5a5; 
 
    border-bottom-right-radius: 4px; 
 
    border-top-right-radius: 4px; 
 
    background: rgba(255, 255, 255, 0.03); 
 
    color: rgba(0, 0, 0, 0.8); 
 
    font-family: "Source Sans Pro", sans-serif; 
 
    margin: 0 auto 50px auto; 
 
    letter-spacing: 0.5px; 
 
    position: relative; 
 
    line-height: 1.4em; 
 
    font-size: 1.03em; 
 
    padding: 30px; 
 
    list-style: none; 
 
    text-align: left; 
 
    font-weight: 100; 
 
    max-width: 30%; 
 
} 
 

 
.timeline h1, 
 
.timeline h2, 
 
.timeline h3 { 
 
    font-family: "Oswald", sans-serif; 
 
    letter-spacing: 1.5px; 
 
    font-weight: 100; 
 
    font-size: 1.4em; 
 
} 
 

 
.timeline .event { 
 
    border-bottom: 1px dashed rgba(255, 255, 255, 0.1); 
 
    padding-bottom: 25px; 
 
    position: relative; 
 
} 
 

 
.timeline .event:last-of-type { 
 
    padding-bottom: 0; 
 
    margin-bottom: 0; 
 
    border: none; 
 
} 
 

 
.timeline .event:before, 
 
.timeline .event:after { 
 
    position: absolute; 
 
    display: block; 
 
    top: 0; 
 
} 
 

 
.timeline .event:before { 
 
    left: -170px; 
 
    color: rgba(0, 0, 0, 1); 
 
    content: attr(data-date); 
 
    text-align: right; 
 
    font-weight: 100; 
 
    font-size: 0.9em; 
 
    min-width: 120px; 
 
} 
 

 
.timeline .event:after { 
 
    box-shadow: 0 0 0 4px #a5a5a5; 
 
    left: -37.85px; 
 
    background: #313534; 
 
    border-radius: 50%; 
 
    height: 11px; 
 
    width: 11px; 
 
    content: ""; 
 
    top: 5px; 
 
} 
 

 
.width-max { 
 
    max-width: 700px; 
 
} 
 

 
.middle-modal { 
 
    top: 40%; 
 
} 
 

 
.speaker { 
 
    display: inline-block; 
 
    padding-right: 15px; 
 
} 
 

 
.speaker img { 
 
    width: 75px; 
 
} 
 

 
.modal-image { 
 
    margin: 1em 1em 0.25em 0; 
 
    border: 1px solid #000000; 
 
    float: left; 
 
    max-width: 100px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
<div class="w3-sand w3-large"> 
 

 

 
    <div class="w3-container " id="schedule"> 
 
    <div class="w3-content width-max "> 
 
     <h4 class="w3-center p2t p1b"><span class="w3-tag w3-wide ">SCHEDULE</span></h4> 
 
    </div> 
 
    <ul class="timeline "> 
 
     <li class="event" data-date="09:30 - 10:00 "> 
 
     <div onclick="document.getElementById('id01').style.display='block'"> 
 
      <h3 class="event-title">Registration</h3> 
 
      <p>It is on first come first server basis. Pls hurry</p> 
 
     </div> 
 
     </li> 
 
     <li class="event" data-date="10:00 - 10:30 "> 
 
     <div> 
 
      <h3 class="event-title">Introduction</h3> 
 
      <p><strong> Speaker </strong> 
 
      <br /> 
 
      </p> 
 
      <div class="speaker"> 
 
      <img src="./avatar.png" alt="Boss" class="w3-circle"> 
 
      <h3>Michael<br />Lee</h3> 
 
      </div> 
 
     </div> 
 
     </li> 
 
     <li class="event" data-date="10:30 - 11:15 "> 
 
     <div> 
 
      <h3 class="event-title">Speech 1</h3> 
 
      <p><strong> Speaker </strong> 
 
      <br /> 
 
      </p> 
 
      <div class="speaker"> 
 
      <img src="./avatar.png" alt="Boss" class="w3-circle"> 
 
      <h3>David<br />Anderson</h3> 
 
      </div> 
 
     </div> 
 
     </li> 
 
     <li class="event" data-date="11:30 - 12:15 "> 
 
     <div> 
 
      <h3 class="event-title">Speech 2</h3> 
 
      <p><strong> Speakers </strong> 
 
      <br /> 
 
      </p> 
 
      <div class="speaker"> 
 
      <img src="./avatar.png" alt="Boss" class="w3-circle"> 
 
      <h3>Steve<br />Newman</h3> 
 
      </div> 
 
     </div> 
 
     </li> 
 
     <li class="event" data-date="12:30 - 13:15 "> 
 
     <div> 
 
      <h3 class="event-title">Competetion</h3> 
 
      <p><strong> Speakers </strong> 
 
      <br /> 
 
      </p> 
 
      <div class="speaker"> 
 
      <img src="./avatar.png" alt="Boss" class="w3-circle"> 
 
      <h3>Mark<br />Smith</h3> 
 
      </div> 
 
     </div> 
 
     </li> 
 
     <li class="event" data-date="14:00 - 14:45 "> 
 
     <div> 
 
      <h3 class="event-title">Prize Distribution</h3> 
 
      <p><strong> Speaker </strong> 
 
      <br /> 
 
      </p> 
 
      <div class="speaker"> 
 
      <img src="./avatar.png" alt="Boss" class="w3-circle"> 
 
      <h3>John<br />Doe</h3> 
 
      </div> 
 
     </div> 
 
     </li> 
 
    </ul> 
 
    </div> 
 

 
    <!-- Modals --> 
 

 
    <div id="id01" class="w3-modal w3-grayscale"> 
 
    <div class="w3-modal-content w3-card-4 w3-animate-opacity middle-modal"> 
 
     <header class="w3-container w3-khaki w3-display-container"> 
 
     <span onclick="document.getElementById('id01').style.display='none'" class="w3-button w3-khaki w3-display-topright"><i class="fa fa-remove"></i></span> 
 
     <h4>Register</h4> 
 
     </header> 
 
     <div class="w3-container bio"> 
 
     <p>Registration link goes here</p> 
 
     </div> 
 
    </div> 
 
    </div> 
 

 
    <div id="id02" class="w3-modal"> 
 
    <div class="vertical-alignment-helper"> 
 
     <div class="vertical-align-center"> 
 
     <div class="w3-modal-content w3-card-4 w3-animate-opacity"> 
 
      <header class="w3-container w3-khaki w3-display-container"> 
 
      <span class="w3-button w3-khaki w3-display-topright close-modal"><i class="fa fa-remove"></i></span> 
 
      <h4></h4> 
 
      </header> 
 
      <div class="w3-container bio"> 
 
      <img alt="Speaker" class="w3-round modal-image"> 
 
      <span></span> 
 
      </div> 
 
     </div> 
 
     </div> 
 
    </div> 
 
    </div> 
 

 
</div>

+0

這個技巧。謝謝 –

0

如果我unterstand正確你不想模態anmiation。它由來自w3.css的類w3-animate-opacity設置。如果您刪除了即時彈出的Modal。

+0

那不正是我所期待的。我確實希望模式能夠像它一樣淡入。但是,當模式打開時,第二次出現的模式中的圖像應該已經存在 –

+0

哦,我明白了。你的意思是Speaker?但是當我打開它時沒有延遲。 –

相關問題