简体   繁体   English

我的fadeToggle()定位到一个我不想要的元素

[英]My fadeToggle() targets an element I don't want it to

I am trying to make a menu that can be hidden after pressing a chevron but when I press it it also hides the chevron. 我试图制作一个可以在按下V形后隐藏的菜单,但是当我按下它时,它也会隐藏V形。 I know why this is (because I target the chevron's parent so it is also affected by fadeToggles' effect) but I have been trying for a while now to come up with a solution. 我知道为什么会这样(因为我以人字形的父对象为目标,所以它也受fadeToggles的效果影响),但是我已经尝试了一段时间了。 I've tried using .not() and adding a div that only encompasses the buttons but to no avail. 我尝试使用.not()并添加仅包含按钮但无济于事的div。 I want to be able to position the elements inside '#FixedMenu' with margin-left. 我希望能够将元素在'#FixedMenu'中定位为margin-left。

http://jsfiddle.net/clarinetking/2PGZS/25/ http://jsfiddle.net/clarinetking/2PGZS/25/

JavaScript/JQuery JavaScript / jQuery

$('#FixedMenu').fadeToggle('slow');
$('#CloseMenu').css({'opacity' : '100'});  

HTML 的HTML

<div id='FixedMenu'>
    <button class='MenuItem'></button>
    <button class='MenuItem'></button>
    <img id='Main' src='http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/800px-Smiley.svg.png'></img>
    <button class='MenuItem'></button>
    <button class='MenuItem'></button>
    <img id='CloseMenu' src='http://upload.wikimedia.org/wikipedia/commons/d/df/Chevron_up_font_awesome.svg'></img>
</div>

Since you need the image inside of #FixedMenu I went about this a different way. 由于您需要#FixedMenu内部的图像, #FixedMenu我采用了另一种方法。

You can't fadeToggle the parent otherwise everything inside will hide. 您不能淡入淡出切换父级,否则里面的所有内容都会隐藏。 What I am doing is adding and removing a class to change the css to have the same effect. 我正在做的是添加和删除一个类,以更改css具有相同的效果。

Here is the jQuery: 这是jQuery:

$(document).ready(function () {
    var position = 0;
    $('#CloseMenu').click(function () {
        position += 180;
        $('#FixedMenu').toggleClass('active');
        $('#Main, .MenuItem').fadeToggle();
        $('#CloseMenu').css({
            '-webkit-transform': 'rotate(' + position + 'deg)',
                '-moz-transform': 'rotate(' + position + 'deg)',
                '-o-transform': 'rotate(' + position + 'deg)',
                '-ms-transform': 'rotate(' + position + 'deg)',
                'transform': 'rotate(' + position + 'deg)'
        });
    });
});

The css I changed, 我更改的CSS,

#FixedMenu.active {
    background: rgba(0, 0, 0, 0.0);
}

Finally, here is a fiddle: JSFIDDLE 最后,这是一个小提琴: JSFIDDLE

Put the image outside div and it will work fine : 将图像放在div之外,它将正常工作:

http://jsfiddle.net/2PGZS/26/ http://jsfiddle.net/2PGZS/26/

<div id='FixedMenu'>
    <button class='MenuItem'></button>
    <button class='MenuItem'></button>
    <img id='Main' src='http://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/800px-Smiley.svg.png'></img>
    <button class='MenuItem'></button>
    <button class='MenuItem'></button>
</div>
<img id='CloseMenu' src='http://upload.wikimedia.org/wikipedia/commons/d/df/Chevron_up_font_awesome.svg'></img>

See if it fits your needs: 查看是否符合您的需求:

jsFiddle jsFiddle

$('#FixedMenu').children().not(this).fadeToggle('slow');

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

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