使用 jQuery 构建类似 Facebook 的类别选择器

发布于 2024-12-05 14:25:21 字数 1644 浏览 0 评论 0原文

我正在尝试构建一个类别选择器,例如 http://www.facebook.com/pages/create .php

你可以在http://jsbin.com/uyupom/2/ ,但是 3 之后脚本变得混乱点击次数。

HTML

  <div id="categories_div">                   
    <div  class="dropbox left">
          <div id="books" class="cat_click is_off">
        Books(click me)
          </div>                        
          <div id="books_input" class="cat_click is_on">
        Some books
          </div>
     </div>

    <div class="dropbox right">
          <div id="movies" class="cat_click is_off">
            Movies (click me)
              </div>
              <div id="movies_input" class="cat_click is_on">
              Some movies
              </div>
        </div>
    </div>

CSS

#categories_div{width:450px;margin:auto;}
.dropbox{height: 110px;width: 220px;background:none #ccc;display: block;overflow:hidden;}
.left{float:left}
.right{float:right}
.cat_click{background:none red;color:#fff;height:110px;}
.is_off{background:none red;color:#fff;height:110px;}

JQUERY

$(document).ready(function() {
$(".cat_click").click(function() {
var $this=$(this);
var id=$this.attr("id");

if($this.hasClass("is_off")){
$(".is_out").slideToggle();
$(".is_out").removeClass("is_out");
$this.slideToggle();

$this.addClass("is_out");
var id2="#" + id + "_input";
$(id2).slideToggle();
}
  });
});

I'm trying to build a category selector like http://www.facebook.com/pages/create.php

You can find my attempt at http://jsbin.com/uyupom/2/ , but the script is getting messy after 3 clicks.

HTML

  <div id="categories_div">                   
    <div  class="dropbox left">
          <div id="books" class="cat_click is_off">
        Books(click me)
          </div>                        
          <div id="books_input" class="cat_click is_on">
        Some books
          </div>
     </div>

    <div class="dropbox right">
          <div id="movies" class="cat_click is_off">
            Movies (click me)
              </div>
              <div id="movies_input" class="cat_click is_on">
              Some movies
              </div>
        </div>
    </div>

CSS

#categories_div{width:450px;margin:auto;}
.dropbox{height: 110px;width: 220px;background:none #ccc;display: block;overflow:hidden;}
.left{float:left}
.right{float:right}
.cat_click{background:none red;color:#fff;height:110px;}
.is_off{background:none red;color:#fff;height:110px;}

JQUERY

$(document).ready(function() {
$(".cat_click").click(function() {
var $this=$(this);
var id=$this.attr("id");

if($this.hasClass("is_off")){
$(".is_out").slideToggle();
$(".is_out").removeClass("is_out");
$this.slideToggle();

$this.addClass("is_out");
var id2="#" + id + "_input";
$(id2).slideToggle();
}
  });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帥小哥 2024-12-12 14:25:21

DEMO

var boxH = $('.box').outerHeight(true);  // get box height (+ padds. & margs.)

$('.dropbox').click(function(){
  $('.opened').stop().animate({top:0},500).removeClass('opened'); // if there's opened boxes
  $(this).find('.box').addClass('opened').stop().animate({top: -boxH},500); // now, the clicked one!     
});

如果您想要悬停操作,只需更改.click.hover :)

HTML ex:

<div id="categories">                   
    <div  class="dropbox">
        <div id="books" class="box">
            <img src="http://img402.imageshack.us/img402/7231/docp.png" alt="Book" />
            <h2>Books</h2>
        </div>                        
        <div id="books_input" class="box desc">
            Some books
        </div>
    </div>
    <!-- ... -->

CSS ex:

body{
    background:#fff;
    font-family:Arial, sans-serif;
}
#categories{
    position:relative;
    margin:0 auto;
    width:557px;height:557px;
}
.dropbox{
    background:#fff;
    position:relative;
    overflow:hidden;
    float:left;
    width:250px;
    height:250px;
    margin:10px;
    border:4px solid #e6e6e6;
}
.box{
    background:#eee;
    position:relative;
    overflow:hidden;
    z-index:2;
    float:left;
    width:200px;
    height:200px;
    margin:5px;
    padding:20px;
    text-align:center;
}
.box img{
    width:128px;
    height:128px;
}
.box h2{
    font-size:34px;
    color:#aaa;
    text-shadow: 1px 2px 2px #fff;
}
.desc{
    text-align:left;
}

DEMO

var boxH = $('.box').outerHeight(true);  // get box height (+ padds. & margs.)

$('.dropbox').click(function(){
  $('.opened').stop().animate({top:0},500).removeClass('opened'); // if there's opened boxes
  $(this).find('.box').addClass('opened').stop().animate({top: -boxH},500); // now, the clicked one!     
});

If you want a hover action, just change .click to .hover :)

HTML ex:

<div id="categories">                   
    <div  class="dropbox">
        <div id="books" class="box">
            <img src="http://img402.imageshack.us/img402/7231/docp.png" alt="Book" />
            <h2>Books</h2>
        </div>                        
        <div id="books_input" class="box desc">
            Some books
        </div>
    </div>
    <!-- ... -->

CSS ex:

body{
    background:#fff;
    font-family:Arial, sans-serif;
}
#categories{
    position:relative;
    margin:0 auto;
    width:557px;height:557px;
}
.dropbox{
    background:#fff;
    position:relative;
    overflow:hidden;
    float:left;
    width:250px;
    height:250px;
    margin:10px;
    border:4px solid #e6e6e6;
}
.box{
    background:#eee;
    position:relative;
    overflow:hidden;
    z-index:2;
    float:left;
    width:200px;
    height:200px;
    margin:5px;
    padding:20px;
    text-align:center;
}
.box img{
    width:128px;
    height:128px;
}
.box h2{
    font-size:34px;
    color:#aaa;
    text-shadow: 1px 2px 2px #fff;
}
.desc{
    text-align:left;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文