![](/img/trans.png)
[英]Math-related Javascript working in all browsers except Internet Explorer
[英]Why is JavaScript styling working with all browsers except Internet Explorer?
我正在使用視頻JS HTML5播放器播放一些視頻,並且想在視頻播放器下方添加章節的直觀表示。
我基本上是從數據庫中獲取多少章,並使用PHP創建該數量的div。
<div class="chapter-progress">
<div class="chapter-breaker"></div>
<? $tableName = 'sourceChapters';
$mysqlQuery = "SELECT * FROM " . $tableName . " WHERE recSourceId = "
.$sourceVideo['recId']. " ORDER BY recOrdering ASC;";
$sourceChapters = mysql_query($mysqlQuery); ?>
<? $chapterBreakerNo = 0; ?>
<? while($chapterBreakers = mysql_fetch_array($sourceChapters)){ ?>
<div class="chapter-breaker-title" id="chapter-breaker-title<?=$chapterBreakerNo?>">
<p><?=$sourceChapters['recContent']?></p></div>
<div class="chapter-breaker" id="chapter-breaker<?=$chapterBreakerNo?>"></div>
<? $chapterBreakerNo++ ?>
<? } ?>
<div class="chapter-breaker-title" id="last-title"></div>
<div class="chapter-breaker" style="float:right"</div>
</div>
因此,有一些章斷開器標題div的樣式將設置為各章的實際大小,並且有章斷開器只是一條線,粗細為1%,用於拆分各章。
然后,我使用JavaScript onload函數為這些div設置樣式。
/* -----------------------------------------------function for styling the chapter progress bar ---------------------------------------*/
function styleChapters(){
//also run the chapter system on load
chapterSys();
// total video duration
var duration = myPlayer.duration();
//find the time difference for the first chapter , this is just the chapter time
var timeDif = (<?=$chapterPlayOffsets[0]?>);
// work out the percentage width for the div width
var percentageMargin = ((timeDif/duration) * 100 -1);
//get the first chapter breaker title and style the width
document.getElementById("chapter-breaker-title0").style.width = percentageMargin + "%";
document.getElementById("chapter-breaker-title0").innerHTML="00";
// for the rest of the chapters
<? for($i=1;$i<count($chapterPlayOffsets);$i++) { ?>
//total video duration
var duration = myPlayer.duration();
// find the difference between the chapter and the previous chapter
var timeDif = (<?=$chapterPlayOffsets[$i]?>) - (<?=$chapterPlayOffsets[$i-1]?>);
// get the percentage for div length minus 1% for the width of the chapter breakers
var percentageMargin = ((timeDif/duration) * 100) -1;
//style each of the divs width
document.getElementById("chapter-breaker-title<?=$i?>").style.width = percentageMargin + "%";
//put the chapter title in
document.getElementById("chapter-breaker-title<?=$i?>").innerHTML="<?= $chapterTitles[$i-1] ?>";
<? } ?>
//put the last title in
<? $lastTitle = count($chapterPlayOffsets)-1 ?>
document.getElementById("last-title").innerHTML="<?=$chapterTitles[$lastTitle] ?>";
}
因此,它應該是這樣的:
問題是,這適用於Internet Explorer以外的所有瀏覽器。
好像div根本沒有設置其寬度樣式。
所以看起來像這樣:
如果任何人都能看到或解釋為什么這可能行不通,那將非常有幫助
.chapter-progress{
width:100%;
height:20px;
background: rgb(255,204,0)
url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAAGCAYAAADgzO9IAAAAP0lEQVQIHWWMAQoAIAgDR/QJ/Ub//04+w7ZICBwcOg5FZi5iBB82AGzixEglJrd4TVK5XUJpskSTEvpdFzX9AB2pGziSQcvAAAAAAElFTkSuQmCC)
-50% 0 repeat;
margin:auto;
float:left;
}
.chapter-breaker{
height:100%;
width:1%;
background-color: rgb(0, 51, 153);
float:left;
}
.chapter-breaker-title{
height:100%;
float:left;
}
div class="chapter-progress">
<div class="chapter-breaker"></div>
<div class="chapter-breaker-title" id="chapter-breaker-title0">
00
</div>
<div class="chapter-breaker" id="chapter-breaker0"></div>
<div class="chapter-breaker-title" id="chapter-breaker-title1">
01
</div>
<div class="chapter-breaker" id="chapter-breaker1"></div>
<div class="chapter-breaker-title" id="chapter-breaker-title2">
02
</div>
<div class="chapter-breaker" id="chapter-breaker2"></div>
<div class="chapter-breaker-title" id="chapter-breaker-title3">
03
</div>
<div class="chapter-breaker" id="chapter-breaker3"></div>
<div class="chapter-breaker-title" id="chapter-breaker-title4">
04
</div>
<div class="chapter-breaker" id="chapter-breaker4"></div>
<div class="chapter-breaker-title" id="last-title">
05
</div>
<div class="chapter-breaker" style="float: right;">
</div>
</div>
的div的輸出對我來說似乎都不錯,但是javascript尚未設置Chapter-breaker-title div的寬度
以防萬一有人遇到類似的問題,我不得不在我的JavaScript上放置時間延遲才能使其正常工作,因此在觸發腳本時,必須在頁面未完全加載之前。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.