jQuery fadeIn()跳转到页面顶部

时间:2012-08-01 01:20:10

标签: jquery

我有自定义jQuery选项卡(不是UI),效果很好。但是,由于选项卡使用fadeIn(),因此当您单击不同的选项卡时,它将跳转到页面顶部。

我已经看过这两个解决方案(下面),似乎设置高度确实解决了跳跃问题。但是,我不想静态设置高度。我试图动态设置高度,但由于某种原因我无法使其工作。我也尝试加载内容(load())但无法使其工作。

jQuery fade making page jump

JQuery fadeIn/fadeOut image on click jumps while loading new image

我的 HTML 代码是:

<ul class="port-tabs">
<li><a href="#tab1">Web Design &amp; Dev</a></li>
<li><a href="#tab2">Graphic Design</a></li>
<li><a href="#tab3">Photography</a></li>
</ul>

<div class="port-slides slides-list tab-container">
<div id="tab1" class="port-subpages tab-content">
    <ul class="portfolio">
  <li>
<div id="port-window">
    <span class="window"></span>
    <span class="gradient"></span>
    <a href="#"><img src="http://localhost:8888/wordpress/wp-content/uploads/2012/07/port-image1.jpg" alt="" title="" width="223" height="225" class="alignnone size-full wp-image-112" /></a>
</div>
  </li>
  <li>
<div id="port-window">
    <span class="window"></span>
    <span class="gradient"></span>
    <a href="#"><img src="http://localhost:8888/wordpress/wp-content/uploads/2012/07/port-image2.jpg" alt="" title="" width="223" height="225" class="alignnone size-full wp-image-112" /></a>
</div>
  </li>
  <li>
<div id="port-window">
    <span class="window"></span>
    <span class="gradient"></span>
    <a href="#"><img src="http://localhost:8888/wordpress/wp-content/uploads/2012/07/port-image3.jpg" alt="" title="" width="223" height="225" class="alignnone size-full wp-image-112" /></a>
</div>
  </li>

  <li>
<div id="port-window">
    <span class="window"></span>
    <span class="gradient"></span>
    <a href="#"><span class="port-just-text">TEXT PLACEHOLDER 1</span></a>
</div>
  </li>
  <li>
<div id="port-window">
    <span class="window"></span>
    <span class="gradient"></span>
    <a href="#"><span class="port-just-text">TEXT PLACEHOLDER 2</a>
</div>
  </li>
  <li>
<div id="port-window">
    <span class="window"></span>
    <span class="gradient"></span>
    <a href="#"><span class="port-just-text">TEXT PLACEHOLDER 3</a>
</div>
  </li>
</ul>
</div>
<p class="clearBoth"></p> <!-- Temporary fix: remove when live -->
<div id="tab3" class="port-subpages tab-content">
    <p>Tab Content 3</p>
</div>

jQuery 代码是这样的:

$(document).ready(function() {

    //When page loads...
    $(".tab-content").hide(); //Hide all content
    $("ul.port-tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab-content:first").show(); //Show first tab content


    //On Click Event
    $("ul.port-tabs li").click(function() {

        $("ul.port-tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab-content").hide(); //Hide all tab content

        var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
        //activeHeight = $(activeTab).height();
        //alert(activeHeight);
        $(activeTab).fadeIn(); //Fade in the active ID content
        return false;
    });
});

有人可以帮助消除点击新标签后跳到顶端吗?

1 个答案:

答案 0 :(得分:0)

我相信这是因为您的锚标签。 尝试添加这些行

  $("ul.port-tabs li").click(function(e) {
  e.preventDefault();
  e.stopPropagation();