2 回答

TA貢獻(xiàn)2051條經(jīng)驗 獲得超10個贊
像這樣的東西?
只需將底部動畫到您想要它去的任何地方。
我在這里使用固定的 px 值作為高度,以便更容易理解正在發(fā)生的事情(至少我希望它更容易)
我從要為其設(shè)置動畫的元素中獲取偏移量(只是最大值)。然后我將電梯的最高值設(shè)置為與目的地的最高值相匹配。
// Called when user selects the Start button
$('#btn-start').click(function() {
// Start the Simulation
let destination = $('#second-floor').offset().top;
$("#elevator").animate( {top: destination}, 200, "linear" );
//});
});
$('.btn-to-floor').on('click', function() {
let floor = $(this).data('floor');
let floors = $('.floor').length;
// eq(floors - floor) needed to so some magic calculations (you could also just use some hardcoded id here based on data attribute.
let destination = $('.floor').eq(floors - floor ).find('.floor-bound').eq(0).offset().top;
$("#elevator").animate( {top: destination}, 200, "linear");
});
.elevator-visual {
width: 55%;
position: relative;
}
.elevator {
position: absolute;
margin-left: 6vw;
bottom: 0;
}
.floor {
height: 180px; /* height of elevator + text + line */
}
.floor-bound {
width: 75%;
}
.floor-title {
margin: 0;
padding: 0;
text-align: right;
color: #777;
margin-right: 6vw;
}
.btn-start{
position: static;
border: none;
padding: 8px 21px;
vertical-align: middle;
text-align: center;
cursor: pointer;
border-radius: 5%;
font-size: 24px;
background-color: #b77110;
color: white;
margin-left: 15px;
margin-top: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Elevator Diagram -->
<div class="elevator-visual">
<div class="floor">
<p id="floor-four-lable" class="floor-title">Floor 4</p>
<hr id="forth-floor" class="floor-bound" />
</div>
<div class="floor">
<p class="floor-title">Floor 3</p>
<hr id="third-floor" class="floor-bound" />
</div>
<div class="floor">
<p class="floor-title">Floor 2</p>
<hr id="second-floor" class="floor-bound" />
</div>
<div class="floor">
<p class="floor-title">Floor 1</p>
<hr id="first-floor" class="floor-bound" />
</div>
<img id="elevator" class="elevator" src="https://via.placeholder.com/150" alt="" />
</div>
<button id="btn-start" class="btn-start">Start</button>
<button class="btn-to-floor" data-floor="1">1</button>
<button class="btn-to-floor" data-floor="2">2</button>
<button class="btn-to-floor" data-floor="3">3</button>
<button class="btn-to-floor" data-floor="4">4</button>

TA貢獻(xiàn)2037條經(jīng)驗 獲得超6個贊
使用 css 的過渡和次要 javascript,您可以通過按鈕的 onclick 的內(nèi)聯(lián) javascript 調(diào)用將您的對象(無論是 img 還是其他,我在演示中使用基于文本的 span)動畫到頁面的一部分 - 我在這篇文章的底部為你寫了一個快速而骯臟的演示。
有關(guān) css 過渡的更多信息:https ://www.w3schools.com/css/css3_transitions.asp
注意:我沒有包含您的隊列列表機(jī)制,因為這不是您的問題的一部分......但是將我的示例實施到您的項目中應(yīng)該不會太難 - 祝你好運(yùn)。
.lift {
position: absolute;
transform: rotate(90deg);
bottom: 10%;
left: 30%;
transition-duration: 2s;
}
.flrfour {
position: absolute;
bottom: 85%;
left: 5%;
}
.flrthree {
position: absolute;
bottom: 60%;
left: 5%;
}
.flrtwo {
position: absolute;
bottom: 35%;
left: 5%;
}
.flrone {
position: absolute;
bottom: 10%;
left: 5%;
}
.buttons {
position: absolute;
bottom: 5px;
left: 50%;
}
<span class="flrfour"> floor 4 </span>
<span class="flrthree"> floor 3 </span>
<span class="flrtwo"> floor 2 </span>
<span class="flrone"> floor 1 </span>
<span id="lft" class="lift">lift</span>
<div class="buttons"><button onclick="document.getElementById('lft').style.bottom = '10%';">1</button><button onclick="document.getElementById('lft').style.bottom = '35%';">2</button><button onclick="document.getElementById('lft').style.bottom = '60%';">3</button><button
onclick="document.getElementById('lft').style.bottom = '85%';">4</button>
</div>
添加回答
舉報