简体   繁体   English

jQuery计算宽度并在悬停时更改同级

[英]jQuery calculate widths and change siblings on hover

The Goal 目标

I want all my list items running horizontally across the page width the total width of all the li equaling 100%. 我希望所有列表项在整个页面宽度上水平运行,所有li的总宽度等于100%。 I then want there to be one active li which is equal to 45% and for the script to calculate what the other li's width would compute once the active is triggered in order to keep the total to within the 100% limit to keep them all running horizontal. 然后,我希望有一个活动的li等于45%,并让脚本计算一旦触发活动,另一个li的宽度将计算什么,以便将总数保持在100%的范围内以保持它们全部运行水平。

I have some code that I'm trying to work it out with. 我有一些代码正在尝试解决。

So far... 至今...

My code — as you will see — will change the width of the hovered element and adjust the children. 如您将看到的,我的代码将更改悬停元素的宽度并调整子元素。 However it only adjusts the children on hoverout . 但是,它仅在hoverout调整孩子。 I can't quite work out how to make this work cleanly. 我无法完全弄清楚如何使它正常工作。 I feel my approach is messy... 我觉得我的方法很混乱...

Help appreciated. 帮助表示赞赏。 See below for code and working example. 请参阅下面的代码和工作示例。

HTML : 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: 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();
});

Live example 现场例子

So, I have updated your code so it is working now. 因此,我已经更新了您的代码,因此现在可以正常工作。 Please have a look at this: http://codepen.io/anon/pen/XdqoaG 请看一下: 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> 

Put the following code after you are assigning 42% width on the hovered item. 在悬停的项目上分配42% width后,输入以下代码。

It's because - first all the items are getting identical width - then when you are increasing size of one - the rest remains as originals. 这是因为-首先所有项目的宽度都相同-然后,当您增加一个项目的尺寸时-其余项目仍保持原始状态。

$li.each(function(){
            if (!$(this).hasClass("is-Active")) {
                $(this).css({
                    "width": 58 / 6 + "%"
                });
            }
        });

you can replace hard-coded 6 by $li.length minus one. 您可以用$li.length减一代替hard-coded 6

As I said in the comments, here is the js, simplified. 正如我在评论中所说,这是简化的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 + "%");
    });    };

And a little bit of css 还有一点CSS

li.is-Active{
        width:40%;
    };

While this does not answer the question, are you aware that you can achieve this with css? 尽管这不能回答问题,但是您知道可以使用CSS来实现吗?

http://jsfiddle.net/4watyjyp/ http://jsfiddle.net/4watyjyp/

ul {
  display: table;
}
li {
  display: table-cell;
}
li:hover {
  width: 45%;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM