jQuery在悬停时计算宽度并更改同级
jQuery calculate widths and change siblings on hover
目标
我希望我的所有列表项在页面宽度上水平运行,所有li的总宽度等于100%。然后,我希望有一个等于45%的活动li,并且脚本计算一旦触发活动li,另一个li的宽度将计算多少,以便将总数保持在100%的限制内,以使它们都保持水平运行。
我有一些代码正在尝试使用它。
到目前为止
正如您将看到的,我的代码将更改悬停元素的宽度并调整子元素。然而,它只调整hoverout
上的子项。我不太清楚如何把这件事做得干干净净。我觉得我的方法很混乱。。。
感谢您的帮助。请参阅下面的代码和工作示例。
HTML:
<div class="tours">
<ul class="tours-List">
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
</ul>
</div>
jQuery:
var
$li = $('.tours li'),
$is_active = false;
function checkActive() {
if (!$li.hasClass("is-Active")) {
$li.css({
"width": 100 / $li.length + "%"
});
} else{
$li.css('width', 58 / ($li.length -1) + "%");
}
}
$li.css({
"float": "left",
"width": 100 / $li.length + "%"
});
$li.hover(function(e){
$is_active = true;
$(this).addClass("is-Active");
$(this).css('width', '42%');
}, function(){
$is_active = false;
$(this).removeClass("is-Active");
checkActive();
});
实例
正如我在评论中所说,这里是经过简化的js。
var $li = $('.tours li');
var widthPercentLeftWhenOneIsActive = 100-40; // you should use constant of course...
init();
function init(){
$li.css({
"float": "left",
"width": 100 / $li.length + "%"
});
};
$li.hover(function(e){
$(this).css("width","");//remove inline style
$(this).addClass("is-Active");
//$(this).css('width', '42%'); move it to the css
shrinkNonActiveItems();
}, function(){
$(this).removeClass("is-Active");
unShrinkAll();
});
function shrinkNonActiveItems(){
$li.each(function(index, item){
if(! $(item).hasClass("is-Active")){
$(item).css("width", (widthPercentLeftWhenOneIsActive / ($li.length-1))+"%" );
}
});
};
function unShrinkAll(){
$li.each(function(index, item){
$(item).css("width",100 / $li.length + "%");
}); };
还有一点css
li.is-Active{
width:40%;
};
在悬停项上分配42% width
之后,放入以下代码。
这是因为,首先所有的物品都有相同的宽度,然后当你增加其中一个的尺寸时,其余的都保持原样。
$li.each(function(){
if (!$(this).hasClass("is-Active")) {
$(this).css({
"width": 58 / 6 + "%"
});
}
});
您可以将hard-coded
6替换为$li.length
减去1。
虽然这并不能回答问题,但你知道用css可以实现这一点吗?
http://jsfiddle.net/4watyjyp/
ul {
display: table;
}
li {
display: table-cell;
}
li:hover {
width: 45%;
}
所以,我已经更新了您的代码,所以它现在可以工作了。请看一下:http://codepen.io/anon/pen/XdqoaG
var
$li = $('.tours li'),
$is_active = false;
function checkActive() {
if (!$li.hasClass("is-Active")) {
$li.css({
"width": 100 / $li.length + "%"
});
} else {
$li.each(function() {
if(!$(this).hasClass("is-Active")){
$(this).css('width', 58 / ($li.length -1) + "%");
}
})
}
}
$li.css({
"float": "left",
"width": 100 / $li.length + "%"
});
$li.hover(function(e) {
$is_active = true;
$(this).addClass("is-Active");
$(this).css('width', '42%');
checkActive();
}, function() {
$is_active = false;
$(this).removeClass("is-Active");
checkActive();
});
.tours{
&-List{
margin: 0;
padding: 0;
li{
list-style: none;
background: {
color: grey;
}
padding: 10em 2em 2em;
box-sizing: border-box;
@for $i from 1 through 7 {
&:nth-of-type(#{$i}) {
background-color: lighten(grey, $i * 3);
}
}
}
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="tours">
<ul class="tours-List">
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
<li>
<h2>Tour Item</h2>
<a href="#">View more</a>
</li>
</ul>
</div>
相关文章:
- 根据元素和容器大小计算边距
- 当鼠标悬停在文本中的单词上时显示警报
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 手风琴可点击并悬停
- 悬停功能触发器
- 如何在鼠标悬停时在另一个图像上滑动图像.
- 鼠标悬停事件影响列表中所有行中的按钮,而不仅仅是特定按钮
- 引导程序:在导航栏中,显示悬停在单个位置的基于Li Link的不同内容
- 使用D3.js计算带有字母间距的文本长度
- css(或jQuery)悬停时淡入淡出
- 使用CSS或JavaScript计算分页符的数量
- jquery快速悬停问题
- jQuery在悬停时计算宽度并更改同级
- 正确的偏移计算,以便在鼠标悬停时显示弹出窗口
- D3.JS鼠标悬停时如何计算某个属性的圆圈数
- 计算用户使用jquery或javascript悬停元素的秒数
- 无法计算悬停元素's的确切位置
- 如何在linux上强制chrome重新计算/重新渲染“:悬停”;造型
- RxJS计算在按钮上悬停的时间