gpt4 book ai didi

javascript - html 元素沿时间轴的均匀间距

转载 作者:行者123 更新时间:2023-11-30 15:58:01 25 4
gpt4 key购买 nike

我正在尝试构建一个可以缩放的时间轴,并在“时间轴”旁边添加间隔均匀的标记,以便用户可以在管理面板中添加额外的时间。

例如,如果有 4 个标记,它们将自动分割为时间线的 1/4,而不管宽度如何。

像这样<--|--|--|--|-->

代码如下:

<style>

body {
background: black;
}
#timeline {
width:49.5%;
background: url(http://brendonwells.co.uk/CPD/ice-training/img/timeline.png) center center no-repeat;
background-size: 100%;
height: 30px;
position: relative;
}

.checkpoint {
position: absolute;
width: 1px;
height: 13px;
top: 13px;
margin-left: auto;
margin-right: auto;
background: white;
cursor: pointer;
}

.checkpoint:hover {
background: white;
}

.checkpoint p {
position: absolute;
height: 30px;
width:0;
bottom: -35px!important;
margin-left: auto;
margin-right: auto;
opacity: 0;
transition: 0.4s;
-o-transition: 0.4s;
-ms-transition: 0.4s;
-moz-transition: 0.4s;
-webkit-transition: 0.4s;
}

.checkpoint:hover p {
opacity: 1;
}

</style>

<div id="timeline">
<div class="checkpoint" >
<div class="rel">
<p>5 Minutes</p>
</div>
</div>

<div class="checkpoint" >
<p>10 Minutes</p>
</div>

<div class="checkpoint" >
<p>15 Minutes</p>
</div>

<div class="checkpoint" >
<p>20 Minutes</p>
</div>

<div class="checkpoint" >
<p>25 Minutes</p>
</div>

<div class="checkpoint" >
<p>30 Minutes</p>
</div>

<div class="checkpoint" >
<p>35 Minutes</p>
</div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>


//Set slide times
var time1 = 300;
var time2 = 600;
var time3 = 900;
var time4 = 1200;
var time5 = 1500;
var time6 = 1800;
var time7 = 2100;

//Array to keep all the times
var times = [time1, time2, time3, time4, time5, time6, time7];

//variable to iterate through all of them
var chosenTime = 0;

//placement needs multiplier
var multiplier = 1;

function deliverTimes() {
$('.checkpoint').each(function() {
$(this).data("time", times[chosenTime]);
//console.log("The data is " + $(this).data('time'));
chosenTime++;
//console.log('running');
placement($(this));
});
}

//Call the function
deliverTimes();

//Clicking checkpoints
$('.checkpoint').click(function() {
video.currentTime = $(this).data("time");
});

//place the checkpoints

function placement($div) {
var width = $('#timeline').width();

var margin = ((width/$('.checkpoint').length) - 10) * multiplier;
console.log(margin);
$div.css("left", margin + "px");
multiplier++;
}
</script>

http://brendonwells.co.uk/timeline.html

我还准备了一个链接,这样 CSS 就可以乱用等等。

谢谢

最佳答案

您可以使用 CSS3 实现您想要的外观 flexbox处理间距和宽度。在 html 和 css 中添加和删除检查点将处理其余部分。不需要 JS。

HTML

<div id="timeline">
<div class="checkpoint">
<div class="rel">
<p>5 Minutes</p>
</div>
</div>

<div class="checkpoint" >
<p>10 Minutes</p>
</div>

<div class="checkpoint" >
<p>15 Minutes</p>
</div>

<div class="checkpoint" >
<p>20 Minutes</p>
</div>

<div class="checkpoint" >
<p>25 Minutes</p>
</div>

<div class="checkpoint" >
<p>30 Minutes</p>
</div>

<div class="checkpoint" >
<p>35 Minutes</p>
</div>
</div>

CSS

body {
background: #000;
}

#timeline {
border-left: 1px solid #fff;
border-right: 1px solid #fff;
color: #fff;
height: 30px;
display: flex;
position: relative;
}
#timeline::after {
content: "";
position: absolute;
width: 100%;
border-top: 1px dashed #fff;
top: 50%;
left: 0;
}
#timeline .checkpoint {
flex: 1;
position: relative;
height: 50%;
margin-top: 15px;
border-right: 1px solid #fff;
}
#timeline .checkpoint:last-child {
border-right: none;
}
#timeline .checkpoint p {
width: 0;
margin: 0;
cursor: pointer;
opacity: 0;
transition: all .3s ease;
}
#timeline .checkpoint p:hover {
opacity: 1;
}

https://jsfiddle.net/s5rL5eja/

关于javascript - html 元素沿时间轴的均匀间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38192931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com