一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

Slide Elements in Different Directions | Learning jQuery

 復(fù)雜網(wǎng)絡(luò)621 2017-04-24

jQuery控制元素上下左右滑動。

Although jQuery has a nice set of slide methods — .slideDown(), .slideUp(), and .slideToggle() — sometimes we may want to slide an element in a different direction. Fortunately, it's pretty easy to do.

Reverse the Slide Direction

With the built-in slide methods, elements are shown by sliding them down and into view. But what if we want to slide something from the bottom up and into view? The trick here is to use some judicious CSS. Let's start with a simple HTML structure:

HTML:
  1. <div id="slidebottom" class="slide">
  2.   <button>slide it</button>
  3.   <div class="inner">Slide from bottom</div>
  4. </div>

To get the inner div to slide up, we'll anchor its bottom edge to the bottom of the bottom of the nearest positioned ancestor (in this case, the #slidebottom div):

CSS:
  1. .slide {
  2.   position: relative;
  3. }
  4. .slide .inner {
  5.   position: absolute;
  6.   left: 0;
  7.   bottom: 0;
  8. }

Other properties such as width, padding, margin, and background-color have been set for these elements, but only the essential properties for modifying the slide behavior are shown above.

Note: I'll be using the term positioned to refer to elements that have the CSS position property set to something other than "static." Both divs in this example are positioned — one absolutely and the other relatively.

Now, we can write the jQuery the same way we would with a traditional slide effect:

JavaScript:
  1. $(document).ready(function() {
  2.   $('#slidebottom button').click(function() {
  3.     $(this).next().slideToggle();
  4.   });
  5. });

Try it out:

Horizontal Slides

Animate Width

We can also slide elements to the left and right. The simplest way is to animate the element's width property.

JavaScript:
  1. $(document).ready(function() {
  2.   $('#slidewidth button').click(function() {
  3.     $(this).next().animate({width: 'toggle'});
  4.   });
  5. });

In this case it's not necessary for the sliding element to be positioned.

Animate this element's width

While animating the width is fine for what it is, I'm not crazy about how the text wraps as the width decreases. One way to avoid the wrapping is to add a CSS declaration such as white-space: nowrap;, but that would mess up the appearance of an element with a lot of text—one in which we would expect to see text wrapping when it is at full length.

Animate Left

Another way to avoid the text-wrap issue is to animate the element's left property. Here, it's important once again to make sure that the element is positioned. After all, we can't move an element if it's static.

With this animation, we need to calculate how far to move the element. The following code rests on two assumptions: (1) the sliding element has an outerWidth() that is equal to or greater than its parent element's width, and (2) the sliding element is initially set to left: 0;. You may have to adjust your code if either one of these assumptions doesn't hold in your situation.

JavaScript:
  1. $(document).ready(function() {
  2.   $('#slideleft button').click(function() {
  3.     var $lefty = $(this).next();
  4.     $lefty.animate({
  5.       left: parseInt($lefty.css('left'),10) == 0 ?
  6.         -$lefty.outerWidth() :
  7.         0
  8.     });
  9.   });
  10. });

Lines 5 through 7 use a "ternary" operator that basically says, "If the left css property equals 0, move the element to the left as many pixels as it is wide (including padding and border); if not, move it back to 0."

Animate this element's left style property

Also, if we want the element to be hidden when it slides to the left, we need to add overflow: hidden; to its parent element.

Animate Left Margin

Finally, we can achieve the same effect as the left animation by animating the marginLeft property. In this case, we still need overflow: hidden; on the parent element, but the sliding element does not need to be positioned.

JavaScript:
  1. $(document).ready(function() {
  2.   $('#slidemarginleft button').click(function() {
  3.     var $marginLefty = $(this).next();
  4.     $marginLefty.animate({
  5.       marginLeft: parseInt($marginLefty.css('marginLeft'),10) == 0 ?
  6.         $marginLefty.outerWidth() :
  7.         0
  8.     });
  9.   });
  10. });

For the sake of variety, we're sliding this one to the right.

Animate this element's margin-left style property

With a couple little tweaks, these horizontal slides can be used for a horizontal accordion.

Further Resources


    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    亚洲欧美日韩国产自拍| 国产又长又粗又爽免费视频| 在线观看免费视频你懂的| 国产精品熟女乱色一区二区| 亚洲免费视频中文字幕在线观看| 欧美日韩亚洲精品内裤| 精品欧美国产一二三区| 日韩人妻免费视频一专区| 亚洲黄色在线观看免费高清| 99精品国产一区二区青青 | 亚洲二区欧美一区二区| 97人妻人人揉人人躁人人| 久久99爱爱视频视频| 青青草草免费在线视频| 国产亚洲欧美一区二区| 国产欧美日韩不卡在线视频| 国产av一二三区在线观看| 国产一区二区三区成人精品| 男女一进一出午夜视频| 亚洲一区二区三在线播放| 国产性色精品福利在线观看| 日本高清视频在线观看不卡| 日本二区三区在线播放| 高清在线精品一区二区| 91人妻人人澡人人人人精品| 一区二区欧美另类稀缺| 日韩一级一片内射视频4k| 大香蕉精品视频一区二区| 中文字幕日韩欧美亚洲午夜| 久久天堂夜夜一本婷婷| 亚洲国产一级片在线观看| 欧美国产在线观看精品| 久久一区内射污污内射亚洲 | 久久精品国产在热久久| 东北女人的逼操的舒服吗| 视频一区二区 国产精品| 国产精品午夜视频免费观看| 亚洲av在线视频一区| 国产精品国三级国产专不卡| 精品国模一区二区三区欧美| 精品推荐国产麻豆剧传媒|