gpt4 book ai didi

javascript - 在无序列表中查找对称元素

转载 作者:行者123 更新时间:2023-11-28 01:22:20 25 4
gpt4 key购买 nike

考虑以下情况:

<ul>
<li></li>
<!-- the minimum amount is at least 7 elements -->
</ul>

令 li 元素的索引为 1 2 3 4 5 6 7

在这种情况下,中间元素是 4。如果我将鼠标悬停在 5、6、7 上,我希望隐藏子列表的第一个元素,并显示列表的下一个元素(如果存在)。

所以如果列表是 1 2 3 4 5 6 7当我悬停 5 6 7 它变成2 3 4 5 6 7 8(出现索引为 8 的元素,索引为 1 的元素显示为:无)现在中间元素变为 5(middleIndex++ 如果 hoveredIndex > middleINdex)现在如果我将鼠标悬停在 2 3 4 上,列表将返回到它的原始状态1 2 3 4 5 6 7.

我现在拥有的是:

$('ul li').mouseover(function()
{
var middleIndex = 3;
var index= $(this).parent().children().index(this);
if(index > middleIndex)
{
// pseudo code
firstElementOfSublist.hide();
followingElementOfSublist.show();
//how do I find the last element of the sublist?
//how do I find the first element of the sublist?
}
}

这是一个 JSFiddle 链接:http://jsfiddle.net/hrapua2y

最佳答案

希望这就是您要找的。

$(document).ready(function () {
var elementsVisible = 7; // elements that are displayed in the list
var maxElements = 10; // max elements in the list
var middleIndex = (elementsVisible - 1) / 2; // the logic is for odd number of elements
var startStack = maxElements - elementsVisible;
var endStack = 0; // presuming that first elements are shown initially
var lock = false;
var lockTime = 10; // how fast to show/hide elements
$('ul#reel li').mouseover(function () {
var index = $(this).parent().children().index(this);
if (index > middleIndex && endStack < 3 && !lock) {
$('ul#reel li:eq(' + (endStack + elementsVisible) + ')').css("display", "inline-block");
$('ul#reel li:eq(' + endStack + ')').css("display", "none");
middleIndex++;
startStack--;
endStack++;
lock = true;
setTimeout(function () {
lock = false;
}, lockTime);
} else if (index < middleIndex && startStack < 3 && !lock) {
$('ul#reel li:eq(' + (endStack - 1) + ')').css("display", "inline-block");
$('ul#reel li:eq(' + (endStack + elementsVisible - 1) + ')').css("display", "none");
middleIndex--;
startStack++;
endStack--;
lock = true;
setTimeout(function () {
lock = false;
}, lockTime);
}
});
});
* {
margin: 0;
padding: 0;
}
ul {
list-style: none;
position:absolute;
left:50%;
top:50%;
margin-left: -375px;
margin-top: -75px;
}
ul li {
display: inline-block;
width: 100px;
height: 100px;
color: red;
font: 25px sans-serif;
text-align: center;
vertical-align: middle;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<body>
<ul id="reel">
<li style="background: #000">1</li>
<li style="background: #333">2</li>
<li style="background: #666">3</li>
<li style="background: #999">4</li>
<li style="background: #ccc">5</li>
<li style="background: #ccc">6</li>
<li style="background: #999">7</li>
<li style="display: none; background: #666">8</li>
<li style="display: none; background: #333">9</li>
<li style="display: none; background: #000">10</li>
</ul>
</body>

jsFiddle

关于javascript - 在无序列表中查找对称元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33503563/

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