1 回答
TA贡献1797条经验 获得超6个赞
让我们假设你的瓶子阵列看起来像下面我的小提琴一样。然后,我首先会以编程方式将瓶子添加到您的页面。从长远来看,当后端的瓶子/图像或数据发生变化时,这将为您节省大量工作。
因此,您需要一些addBottle功能来将瓶子显示在您的“架子”上。mouseleave在该函数中,您为和 的瓶子附加事件处理程序mouseenter。
let bottles = [
{img: 'someImageReference 1', title: 'some Title 1', year: '1900'},
{img: 'someImageReference 2', title: 'some Title 2', year: '1901'},
{img: 'someImageReference 3', title: 'some Title 3', year: '1902'},
{img: 'someImageReference 4', title: 'some Title 4', year: '1903'},
{img: 'someImageReference 5', title: 'some Title 5', year: '1904'},
{img: 'someImageReference 6', title: 'some Title 6', year: '1905'},
{img: 'someImageReference 7', title: 'some Title 7', year: '1906'},
{img: 'someImageReference 8', title: 'some Title 8', year: '1907'},
];
for(var i = 0; i < bottles.length; i++) {
addBottle(bottles[i]);
}
function addBottle( bottleData ) {
let $bottle = $('<span></span>').addClass('bottle');
let $shelf = $('.shelf');
$shelf.append($bottle);
$bottle.on('mouseenter', function() {
displayBottleData( bottleData );
}).on('mouseleave', function() {
hideBottleDetails();
});
}
function hideBottleDetails() {
let $bartender = $('#bartender');
$bartender.empty();
}
function displayBottleData( data ) {
let $bartender = $('#bartender');
hideBottleDetails();
$bartender.text('This bottle is from ' + data.year);
}
#bartender {
width: 100%;
background: red;
margin-top: 2em;
color: white;
}
.shelf {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.bottle{
width: 10%;
height: 100px;
background: black;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="shelf"></div>
<div id="bartender">
</div>
- 1 回答
- 0 关注
- 89 浏览
添加回答
举报