Jquery只滑动DIV页面的一半

时间:2013-11-20 05:11:54

标签: javascript jquery css jquery-ui jquery-ui-slider

我尝试制作微软支持网站上的内容。查看 - > (Visit Here

点击其中列出的任何产品。 (注意:本文的目的/问题不是任何产品的营销!!!只是为了知识。)

enter image description here

当您点击其中任何一个时,会出现一个下拉列表。

enter image description here

现在,我的问题点出来了。点击菜单项后,您可以看到有一个"选择一个主题"列表,当您点击其中任何一个时,第二个列表出来。点击第二个列表中的项目后,第三个列表出来。我确实尝试制作这样的脚本。我通过互联网搜索并制作一个div滑块。但它滑动整个div。 这是我的代码我做了什么

HTML

<div id="gallery">
  <div id="slider">
     <div style="background:#cf5">1</div>
     <div style="background:#ada">2</div>
     <div style="background:#b0b">3</div>
  </div>
  <span id="prev">Prev</span>
  <span id="next">Next</span>
</div>

CSS

#gallery{
  position:relative;
  margin: 0 auto;
  overflow:hidden;
  width:500px;
  height:330px; /* +30 = space for buttons */
  text-align:center; /* to center the buttons */
}
#slider{
  position:absolute;
  left:0;
  height:300px;  
  text-align:left; /* to reset text inside slides */
}
#slider > div {
  position:relative;
  float:left;
  width:500px;
  height:300px;
}
#prev, #next{
  display:inline-block;
  position:relative;
  top:300px;
  cursor:pointer;
  padding:5px;
}

Jquery的

$(function(){

    var $gal = $('#gallery'),
    $sli = $('#slider'),
    $box = $('div',$sli),
    W    = $gal.width(), // 500
    N    = $box.length,  // 3
    C    = 0;            // a counter

    $sli.width(W*N);

    $('#prev, #next').click(function(){
    C = (this.id=='next' ? ++C : --C) <0 ? N-1 : C%N;
    $sli.stop().animate({left: -C*W },800);
    }); 

});

以下是我在 Fiddle

上的代码示例

1 个答案:

答案 0 :(得分:0)

做了一些R&amp; D之后。我做了类似的东西

<强> HTML

<div>
        <input type="submit" id = "submit" value = "Show panel"/>
        <span id = "showpanel1"></span>
    </div>

    <div id = "slider"  style = "display:none">
        <div class = "panel1" style = "display:none">
            <ul>
                <li id = "divpanel0">Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
                <li id = "divpanel1">Pellentesque nec est eget eros placerat imperdiet sed ac purus.</li>
                <li id = "divpanel2">Fusce id dui lacinia, scelerisque dolor vitae, faucibus sem.</li>
                <li id = "divpanel3">Nulla dignissim odio non turpis consectetur malesuada.</li>
                <li id = "divpanel4">Ut vestibulum est quis lacinia sagittis.</li>
                <li id = "divpanel5">Maecenas interdum libero at suscipit iaculis.</li>
                <li id = "divpanel6">Donec in nibh sed lacus ultrices pellentesque sed in purus.</li>
                <li id = "divpanel7">Aliquam luctus eros id semper vestibulum.</li>
                <li id = "divpanel8">Donec vitae felis at leo rutrum mattis.</li>
            </ul>
        </div>

        <div class = "panel2" style = "display:none">
            <ul>
                <li id = "divpanel0">Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
                <li id = "divpanel1">Pellentesque nec est eget eros placerat imperdiet sed ac purus.</li>
                <li id = "divpanel2">Fusce id dui lacinia, scelerisque dolor vitae, faucibus sem.</li>
                <li id = "divpanel3">Nulla dignissim odio non turpis consectetur malesuada.</li>
                <li id = "divpanel4">Ut vestibulum est quis lacinia sagittis.</li>
                <li id = "divpanel5">Maecenas interdum libero at suscipit iaculis.</li>
                <li id = "divpanel6">Donec in nibh sed lacus ultrices pellentesque sed in purus.</li>
                <li id = "divpanel7">Aliquam luctus eros id semper vestibulum.</li>
                <li id = "divpanel8">Donec vitae felis at leo rutrum mattis.</li>
            </ul>
        </div>

    </div>


    <!-- Load Loder GIF -->


        <div id = "loader" style = "display:none">
            <img src = "loader.gif">
        </div>

    <!-- QnA Div Start -->

    <div id = "qaslider"  style = "display:none">
        <div class = "mainpanel" style = "display:none">
            <ul>
                <li id = "divpanel0">Lorem ipsum dolor sit amet, consectetur adipiscing elit</li>
                <li id = "divpanel1">Pellentesque nec est eget eros placerat imperdiet sed ac purus.</li>
                <li id = "divpanel2">Fusce id dui lacinia, scelerisque dolor vitae, faucibus sem.</li>
                <li id = "divpanel3">Nulla dignissim odio non turpis consectetur malesuada.</li>
                <li id = "divpanel4">Ut vestibulum est quis lacinia sagittis.</li>
                <li id = "divpanel5">Maecenas interdum libero at suscipit iaculis.</li>
                <li id = "divpanel6">Donec in nibh sed lacus ultrices pellentesque sed in purus.</li>
                <li id = "divpanel7">Aliquam luctus eros id semper vestibulum.</li>
                <li id = "divpanel8">Donec vitae felis at leo rutrum mattis.</li>
            </ul>
        </div>


    </div>

<强> JS

<script src="js/jquery-1.9.1.js"></script>
  <script src="js/jquery-ui.js"></script>
  <script>  
    $(document).ready(function() {
         var firsttext;
         $(".panel1").on('click','li',function (){
            $(".panel2").show("slide", { direction: "right" }, 1000);
            $("span").text($(this).html());
            firsttext = $(this).html();

        });

        $('#submit').click(function() {
            $(".mainpanel").hide();
            $("#slider").show("slide", { direction: "right" }, 0);
            $(".panel2").hide("slide", { direction: "right" }, 0);
            $(".panel1").show("slide", { direction: "right" }, 1000);
        });

        $(".panel2").on('click','li',function (){
            $("span").text(firsttext + " > " + $(this).html());
            $( "#slider" ).fadeOut( "slow" );
            $("#loader").show("slide", { direction: "right" }, 800);
            $("#loader").center(true);
            setTimeout(removeslider,4000)
        });

    });

    function removeslider()
    {
        $("#loader").hide("slide", { direction: "left" }, 800);
        $("#qaslider").show("slide", { direction: "right" }, 0);
        $(".mainpanel").show("slide", { direction: "right" }, 1000);
    }
    // Custome Jquery Function to stop/place element at center screen

    jQuery.fn.center = function(parent) {
        if (parent) {
            parent = this.parent();
        } else {
            parent = window;
        }
        this.css({
            "position": "absolute",
            "top": ((($(parent).height() - this.outerHeight()) / 2) + $(parent).scrollTop() + "px"),
            "left": ((($(parent).width() - this.outerWidth()) / 2) + $(parent).scrollLeft() + "px")
        });
        return this;
    }

  </script>

<强>风格

<style>
    .panel1 .panel1
    {
        border:1px solid black;
        margin-right:800;
        margin-top:20px;
    }
    #loader
    {
        margin-right:708;
        margin-top:117px;
        margin-left:525px;
    }
    .panel1 ul li:hover, .panel2 ul li:hover
    {
        cursor:hand
    }
    #slider > div {
      position:relative;
      float:left;
      width:500px;
      height:300px;
    }
  </style>