javascript - 使用 jquery 切换幻灯片

标签 javascript css slidetoggle article

我有以下 javascript,它允许我的页面访问者单击链接并显示文章的全部内容,例如“阅读更多”,当您单击它时,整篇文章会显示在下面。

我想要的是当你点击“阅读更多”时,要滑下来的文章不只是出现。

提前谢谢你们。

HTML

<p>The text that is shown by default.<a href="#" id="example-show" class="showLink" onclick="showHide('example');return false;">Read more</a></p>
      <div id="example" class="more">
         <p>The rest of the article</p>
         <p><a href="#" id="example-hide" class="hideLink" onclick="showHide('example');return false;">Hide this content.</a></p>

CSS

.more {
      display: none;
      border-top: 1px solid #666;
      border-bottom: 1px solid #666; }
   a.showLink, a.hideLink {
      text-decoration: none;
      color: #336699;
      padding-left:20px;
      background: transparent url(down.gif) no-repeat left; }
   a.hideLink {
      background: transparent url(up.gif) no-repeat left; }
   a.showLink:hover, a.hideLink:hover {
      border-bottom: 1px dotted #36f; }

JavaScript

  <script language="javascript" type="text/javascript">
function showHide(shID) {
   if (document.getElementById(shID)) {
      if (document.getElementById(shID+'-show').style.display != 'none') {
         document.getElementById(shID+'-show').style.display = 'none';
         document.getElementById(shID).style.display = 'block';
      }
      else {
         document.getElementById(shID+'-show').style.display = 'inline';
         document.getElementById(shID).style.display = 'none';
      }
   }
}
</script>

最佳答案

使用 jQuery,您可以:

$(function () {
    $('.showLink').click(function (e) {
        e.preventDefault();
        $('#example').slideToggle();
    });

    $('.hideLink').click(function (e) {
        e.preventDefault();
        $('#example').slideUp();
    });
});

Fiddle Demo

关于javascript - 使用 jquery 切换幻灯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22534535/

相关文章:

javascript - JS 添加回调来运行下一个函数?

javascript - 将indexOf() 值加一

javascript - 如何避免javascript倒计时器不走负数?

html - HTML中的LINK CSS在网上有没有用?一切都好

css - 方向奇怪的行为 : rtl and closing braces at the end of the sentence

jquery SlideToggle() 和未知高度?

javascript - 避免在每次部署后缓存 JS、CSS 文件页面

html - 面包屑事件链接 css

javascript - 显示 block 添加滑动效果不显示div

javascript - 向下滑动菜单 - 如何一次只允许打开一条路径?