具有多个 Div 的 jQuery 切换/垂直滑块效果
实际上,我是 jQuery 的新手,我正在使用 jQuery 函数编写一个示例页面。在此页面中,我使用切换功能。请帮助我达到预期的效果。我正在尝试获取它,但它无法正常工作。 我想要应用的效果是;
- 页面上有 2 个按钮,即 Button1 和 Button2。
- 有 2 个 Div,即 Div1 和 Div2。
- 最初,两个 Div 都是隐藏的,只有 2 个按钮可见。
- 如果单击 Button1,Div1 应该向下切换,反之亦然。
- 如果 Div1 处于打开状态,并且单击 Button2,则 Div1 应该稍微上升,Div2 应该下降。
我写了一些应用了 CSS 的代码。但我没有得到滑动效果,因为它只提供了一个 Div。
我将 Javascript 写为:
var IsPanelDown = false;
var IsPanel2Down = false;
$(document).ready(function() {
// Expand Panel
$("#open").click(function(){
if (IsPanel2Down == false)
{
$("div#panel2").slideUp("slow");
IsPanel2Down = false;
}
$("div#panel").slideDown("slow");
IsPanelDown = true;
});
// Collapse Panel
$("#close").click(function(){
$("div#panel").slideUp("slow");
IsPanelDown = false;
});
// Switch buttons from "Log In | Register" to "Close Panel" on click
$("#toggle a").click(function () {
$("#toggle a").toggle();
});
$("#toggle2 a").click(function () {
$("#toggle2 a").toggle();
});
$("#open1").click(function(){
if(IsPanelDown == false)
{
$("div#panel").slideUp("slow");
IsPanelDown = false;
}
$("div#panel2").slideDown("slow");
IsPanel2Down = true;
});
// Collapse Panel
$("#close1").click(function(){
$("div#panel2").slideUp("slow");
IsPanel2Down = false;
});
});
Actually, I am new to jQuery and I am writting a sample page with jQuery functions. In this page I am making use of toggle function. Please help me to get the intended effect. I am trying to get it but it is not working properly.
The effect I am trying to apply is;
- There are 2 buttons on the page, say Button1 and Button2.
- There are 2 Divs, say Div1 and Div2.
- Initially both Divs are hidden and only 2 buttons are visible.
- If Button1 is clicked, Div1 should get toggle down and vice versa.
- If Div1 is in open state and Button2 is clicked, Div1 should go up slightly and Div2 should fall down.
I have written some code with CSS applied on it. But I am not getting the sliding effect as it gives with only one Div.
I have written Javascript as;
var IsPanelDown = false;
var IsPanel2Down = false;
$(document).ready(function() {
// Expand Panel
$("#open").click(function(){
if (IsPanel2Down == false)
{
$("div#panel2").slideUp("slow");
IsPanel2Down = false;
}
$("div#panel").slideDown("slow");
IsPanelDown = true;
});
// Collapse Panel
$("#close").click(function(){
$("div#panel").slideUp("slow");
IsPanelDown = false;
});
// Switch buttons from "Log In | Register" to "Close Panel" on click
$("#toggle a").click(function () {
$("#toggle a").toggle();
});
$("#toggle2 a").click(function () {
$("#toggle2 a").toggle();
});
$("#open1").click(function(){
if(IsPanelDown == false)
{
$("div#panel").slideUp("slow");
IsPanelDown = false;
}
$("div#panel2").slideDown("slow");
IsPanel2Down = true;
});
// Collapse Panel
$("#close1").click(function(){
$("div#panel2").slideUp("slow");
IsPanel2Down = false;
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SlideUp/SlideDown 控制可见性,而不是定位。使用 CSS 或文档结构来控制两个 DIV 可见时的相对位置。
SlideUp/SlideDown control visibility, not positioning. Use CSS or the document structure to control the relative positioning of the two DIVs when they are visible.
它没有经过测试,但尝试这样的事情
It's not tested but try something like this
假设您有与此类似的标记:
然后像这样使用 jQuery:
有很多方法可以做到这一点 - 这在某种程度上取决于您的标记!
Assuming you've got similar markup to this:
Then use jQuery like this:
There are loads of ways to do this - it sort of depends on your markup!
我对 jquery 也和你一样陌生,但我想如果你使用 live property 它会对你有帮助,如下所示:
I'm also as new as you are to jquery, but i guess if you used live property it would help you, like this :