内容在 jQuery 下拉动画完成之前出现

发布于 2024-11-07 10:56:59 字数 972 浏览 0 评论 0原文

单击“阅读更多”链接后,我使用 jQuery 向下滑动并显示页面上的内容。动画效果很好,但在单击链接后、动画幻灯片之前立即显示正在显示的内容。有趣的是,我在其他一些网站上使用过这段代码,但从未遇到过这个问题。

这是代码:

<script type="text/javascript">

function ShowHide(){
$(".details").animate({"height": "toggle"}, { duration: 500 });
}

</script>

和 html

<div class="readmore"><a href="#" onclick="ShowHide(); return false;">More Info</a></div>            
    <div class="details">
        <div class="description">
         <div class="title">Description</div>
          <p>content here</p>

         <div class="title">Subscribe</div>
         <a href="#">lorem</a><br />
         <a href="#">lorem</a>
        </div>
    </div> 

任何帮助都会很棒!另外,我可能希望在一页上有此幻灯片的多个实例。现在单击“阅读更多”链接将显示所有“详细信息”div。我将如何使这项工作仅在单个 div 上进行,而不必为每个实例编写新代码。我想要的一个例子是一个 WordPress 网站,它显示帖子列表中帖子的详细信息。

I'm using jQuery to slidedown and reveal content on my page after a "read more" link is clicked. The animation works well but the content that is being revealed appears immediately after the link is clicked, before the animated slidedown. The funny part is that I've used this code on a few other sites and never had this issue.

Here's the code:

<script type="text/javascript">

function ShowHide(){
$(".details").animate({"height": "toggle"}, { duration: 500 });
}

</script>

and the html

<div class="readmore"><a href="#" onclick="ShowHide(); return false;">More Info</a></div>            
    <div class="details">
        <div class="description">
         <div class="title">Description</div>
          <p>content here</p>

         <div class="title">Subscribe</div>
         <a href="#">lorem</a><br />
         <a href="#">lorem</a>
        </div>
    </div> 

Any help would be great! Also on a side note, I may want to have multiple instances of this slidedown on one page. Right now clicking the "read more" link would reveal all the "details" divs. How would I go about making this work just on a single div without having to write new code for each instance. An example where I would want that would be a wordpress site that reveals details from a post in a list of posts.

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

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

发布评论

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

评论(1

沧笙踏歌 2024-11-14 10:56:59

确保包含内容的 div(在您的情况下为“details”类)也具有 overflow:hidden CSS 样式集。

要回答你的第二个问题,你可以这样做:

$(document).ready(function() 
{ 
    //Bind the ShowHide function to onclick for each first link in the "readmore" div
    $('div.readmore > a').click(ShowHide);
});

function ShowHide(event)
{
    //Find the next "details" div sibling below the link container and animate it
    $(event.target).parent('div.readmore').next('div.details').animate(
        {"height": "toggle"}, { duration: 500 });
}

另外,请确保在使用它时删除分配给链接的内联“onclick”处理程序。

Make sure that the div containing the content (in your case the one with the class "details") also has the overflow: hidden CSS style set.

To answer your second question, you could do it as follows:

$(document).ready(function() 
{ 
    //Bind the ShowHide function to onclick for each first link in the "readmore" div
    $('div.readmore > a').click(ShowHide);
});

function ShowHide(event)
{
    //Find the next "details" div sibling below the link container and animate it
    $(event.target).parent('div.readmore').next('div.details').animate(
        {"height": "toggle"}, { duration: 500 });
}

Also make sure to remove the inline "onclick" handler assigned to the link when using this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文