jquery slideToggle direction

≯℡__Kan透↙ 提交于 2019-12-05 00:25:53

You need to add a parent to the text.

<body>
   <div class="nav">Nav</div>
   <div class="text">
      <p>This is the paragraph to end all paragraphs. You should feel <em>lucky</em> to have seen such a paragraph in your life. Congratulations!</p>
   </div>
</body>

Style:

body {
   font-size: 10px;
   color: #333; }

.nav {
   float: right;
   width: 50px;
   height: 30px;
   text-align: center;
   background: #ccc;
   border: 1px solid #333; }

.text {
   overflow: hidden;
   float: right;
   margin: 5px;
   padding: 5px;
   width: 400px;
   background: #e4e4e4; }

And the javascript:

$('.nav').click(function() {
   $('.text p').css({
      'width': $('.text p').width(),
      'height': $('.text p').height()
   });
   $('.text').animate({'width': 'toggle'});
});

You have a demo at: http://jsfiddle.net/abwVd/

The default behaviour of slideToggle in jquery is to "slide up" or "slide down". If you want your <p> to "slide left" and "slide right", you could use the 'slide' effect available in jquery ui. The direction argument accepts 'up','down','left','right' as valid values. http://docs.jquery.com/UI/Effects/Slide

You could do this:

p { width:400px; position: absolute; background:#e4e4e4; margin:5px;padding:5px;}

(use position absolute instead of float right)

And this:

 $(document).ready(function() {
    var $elem = $("p");           
    var docwidth =  $(document).width();
    var inileft = docwidth - $elem.outerWidth();
    $elem.css("left", inileft);  //Position element tho the right
    $("div").click(function () {  //This slides
      $elem.animate({
        left: (parseInt($elem.css("left"), 10) >= docwidth ?  inileft : docwidth)
      });
   });
 });

By using animate, you can do very custom animations. Hope this helps. Cheers

user2285869
<script>

    $("div").click(function () {

      $("p").slideToggle("slow", {direction: "left"}, 100);


    });

</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!