简体   繁体   English

单击使div向下滑动

[英]Getting a div to slideDown on click

I am wanting a hidden div to slide down from the top of the screen when the user clicks on "Services". 我希望当用户单击“服务”时,隐藏的div从屏幕顶部向下滑动。

When the div does slide down, I want the height to be height: 35vh; 当div确实滑落时,我希望高度为height: 35vh; .

I am unsure of what I am doing wrong. 我不确定自己在做什么错。 Does anyone see the issue? 有人看到这个问题吗?

 $('#serviceClick').click( function () { $('serviceNav').addClass('active').slideDown(); console.log('The click is working'); }); 
 #serviceNav { width: 100%; top: -35vh; z-index: -1; position: absolute; background-color: rgba(0,0,0,0); } #serviceNav.active { height: 35vh; top: 0; width: 100%; background: red; z-index: 500; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <li id="serviceClick">SERVICES</li> <div id="serviceNav"></div> 

You forgot to add # in front of the ID 您忘记在ID前面添加

 $('#serviceClick').click( function () { $('#serviceNav').addClass('active').slideDown(); console.log('The click is working'); }); 
 #serviceNav { width: 100%; top: -35vh; z-index: -1; position: absolute; background-color: rgba(0,0,0,0); } #serviceNav.active { height: 35vh; top: 0; width: 100%; background: red; z-index: 500; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <li id="serviceClick">SERVICES</li> <div id="serviceNav"></div> 

Firstly - You need to reference the ID - $('#serviceNav') . 首先-您需要引用ID- $('#serviceNav') Secondly - Why are you sliding down and adding a class which moves it anyway? 其次-为什么要下滑并添加一个无论如何都会移动它的类? Thirdly - You can't animate the height element, you should use max-height . 第三-无法设置height元素的动画,应使用max-height

$('#serviceClick').click( function () {
        $('#serviceNav').addClass('active');
        console.log('The click is working');
});


#serviceNav {
    width: 100%;
    top: -35vh;
    z-index: -1;
    position: absolute;
    background-color: rgba(0,0,0,0);
    height: 35vh;
    display: none;
}
#serviceNav.active {
    top: 0;
    width: 100%;
    background: red;
    z-index: 500;
}

Edit - example: https://jsfiddle.net/664chupo/ 编辑-示例: https : //jsfiddle.net/664chupo/

Here is a working code and codepen. 这是工作代码和Codepen。

HTML 的HTML

   <li id="serviceClick">SERVICES</li>
    <div id="serviceNav">text</div>

CSS 的CSS

#serviceNav {
    width: 100%;
    top: -35vh;
    z-index: -1;
    position: absolute;
    background-color: rgba(0,0,0,0);
}
#serviceNav {
    height: 0vh;
    top: 0;
    width: 100%;
    background: red;
    z-index: 500;
  display:none;
  transition: all 2s;
}

JS JS

$('#serviceClick').click( function () {
        $('#serviceNav').show();
  $('#serviceNav').css('height','35vh');
});

Codepen url: codepen Codepen网址: codepen

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

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