简体   繁体   English

扩展和收缩元素

[英]Expanding and contracting elements

My overall goal of this is to click on a and it expands to the full width of the page, the other 2 's would fadeOut. 我的总体目标是单击a,它会扩展到页面的整个宽度,其他2个将淡出。 When the Active is then clicked it would go back to its initial state and the other 2 's would fadeIn. 然后单击“活动”,它将返回到其初始状态,其他2将淡入。

I have created a JSFiddle here: http://jsfiddle.net/tJugd/3534/ 我在这里创建了一个JSFiddle: http : //jsfiddle.net/tJugd/3534/

HTML: HTML:

<div id="staff" class="slide" style="height:568px;">
<div class="staff1" data-hammer="[object Object]" style="-webkit-user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); touch-action: none; -webkit-transform-origin: 0px 0px 0px; opacity: 1; -webkit-transform: scale(1, 1);"></div>

<div class="staff2" data-hammer="[object Object]" style="-webkit-user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); touch-action: none; -webkit-transform-origin: 0px 0px 0px; opacity: 1; -webkit-transform: scale(1, 1);"></div>

<div class="staff3" data-hammer="[object Object]" style="-webkit-user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); touch-action: none; -webkit-transform-origin: 0px 0px 0px; opacity: 1; -webkit-transform: scale(1, 1);"></div>
</div>

CSS: CSS:

.staff1{
    width:33%;
    height:568px;
    background:red;
    float: left;
}
.staff2{
    width:34%;
    height:568px;
    background:blue;
    float: left;
}
.staff3{
    width:33%;
    height:568px;
    background:yellow;
    float: left;
}

JS: JS:

$('div').click(function(){
    $(this).animate({width:'100%'})
})

Any help would be awesome! 任何帮助都是极好的!

HTML : HTML:

<div id="staff" class="slide" style="height:568px;">
<div class="staff staff1" data-hammer="[object Object]" style="-webkit-user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); touch-action: none; -webkit-transform-origin: 0px 0px 0px; opacity: 1; -webkit-transform: scale(1, 1);"></div>
<div class="staff staff2" data-hammer="[object Object]" style="-webkit-user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); touch-action: none; -webkit-transform-origin: 0px 0px 0px; opacity: 1; -webkit-transform: scale(1, 1);"></div>
<div class="staff staff3" data-hammer="[object Object]" style="-webkit-user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); touch-action: none; -webkit-transform-origin: 0px 0px 0px; opacity: 1; -webkit-transform: scale(1, 1);"></div>
</div>

CSS: CSS:

.staff {
    width:33%;
    height:568px;
    float: left;
    transition : width 0.3s;
}
.staff1{
    background:red;
}
.staff2{
    width:34%;
    background:blue;
}
.staff3{
    background:yellow;
}
.full {width: 100%}

JS: JS:

$('div').click(function(){
    $(".selected").removeClass("selected").removeClass("full");
    $(this).addClass("selected");
    $(".staff:not(.selected)").fadeOut(400, function(){
        $(".selected").addClass("full")
    })
})

Should work 应该管用

I added a common class to all three staff divs and add a class "full" with 100% width. 我向所有三个职员div添加了一个普通类,并添加了100%宽度的“完整”类。 As I added transition to .staff on width, it should be animated, finally, I put the code where the full comes full on fadeOut callback to prevent buggy stuff. 当我在宽度上添加到.staff的过渡时,应该对其进行动画处理,最后,我将代码放置在fadeOut回调的完整内容中,以防止发生错误。

I would change the listener to only listen to div s with class staff in them. 我将更改监听器,使其仅监听其中包含班级职员的div I'd get the class name and width. 我会得到班级的名称和宽度。 If the width is 100% it's already open so I just need to make all divs width 33%. 如果宽度为100%,它已经打开,所以我只需要使所有divs宽度为33%。 Otherwise its 33% and should go to 100% while the rest go to 0%. 否则,其值为33%,应达到100%,而其余的为0%。

 $('div[class*="staff"]').click(function(){ var className=$(this).attr('class'); var width=$(this).attr('style'); width=width.substring(width.indexOf('width')+7); if(width!='100%;'){ $('div[class!="'+className+'"][class*="staff"]').each(function(){ $(this).animate({width:'0%'}); }); $(this).animate({width:'100%'}); } else{ $('div[class*="staff"]').each(function(){ $(this).animate({width:'33%'}); }); } }); 
 .staff{ width:100%; } .staff1{ width:33%; height:568px; background:red; float: left; } .staff2{ width:34%; height:568px; background:blue; float: left; } .staff3{ width:33%; height:568px; background:yellow; float: left; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> <div id="staff" class="slide" style="height:568px;"> <div class="staff1" data-hammer="[object Object]" style="-webkit-user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); touch-action: none; -webkit-transform-origin: 0px 0px 0px; opacity: 1; -webkit-transform: scale(1, 1);"></div> <div class="staff2" data-hammer="[object Object]" style="-webkit-user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); touch-action: none; -webkit-transform-origin: 0px 0px 0px; opacity: 1; -webkit-transform: scale(1, 1);"></div> <div class="staff3" data-hammer="[object Object]" style="-webkit-user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); touch-action: none; -webkit-transform-origin: 0px 0px 0px; opacity: 1; -webkit-transform: scale(1, 1);"></div> </div> 

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

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