jQuery fade 使页面跳转

发布于 2024-09-16 12:35:18 字数 861 浏览 7 评论 0原文

由于某种原因,jQuery 的 fadeIn 使我的页面跳转到顶部。它淡入的每个不同的 div 都会使滚动条变得不同的大小,所以我认为这可能就是“return false”不起作用的原因。这是代码:

    jQuery(document).ready(function($) {

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

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

    $("ul.tabs li").removeClass("active"); //Remove any "active" class
    $(this).addClass("active"); //Add "active" class to selected tab
    $(".tab_content").hide();
    var activeTab = $(this).find("a").attr("href");
    $(activeTab).fadeIn('slow', function() {
  $(this).show(); });
return false
});
});

如果有人可以提供帮助,我将不胜感激。这是网站:

www.matthewruddy.com/demo

这是主要内容上方的选项卡式链接。每一篇文章都会消失在该类别的前五名帖子中。

提前致谢。马修.

For some reason jQuery's fadeIn is making my page jump to the top. Each different div it fades in makes the scroll bar go a different size so I think this might be why 'return false' isn't working. Here is the code:

    jQuery(document).ready(function($) {

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

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

    $("ul.tabs li").removeClass("active"); //Remove any "active" class
    $(this).addClass("active"); //Add "active" class to selected tab
    $(".tab_content").hide();
    var activeTab = $(this).find("a").attr("href");
    $(activeTab).fadeIn('slow', function() {
  $(this).show(); });
return false
});
});

I would appreciate if anyone could help out. Here is the site :

www.matthewruddy.com/demo

It's the tabbed links above the main content. Each one fades in the top five posts from that category.

Thanks in advance. Matthew.

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

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

发布评论

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

评论(5

澜川若宁 2024-09-23 12:35:18

问题不在于链接,尽管我明白这是第一个想法,而是过渡本身。

在一瞬间(一帧,准确地说是 13 毫秒,在隐藏和淡入的第一帧之间),选项卡面板所在区域没有内容,因此页面向上滚动,因为该文件较短。

为了避免这种情况,您需要防止文档变小,幸运的是,对于您的特定页面来说,这非常容易。只需将其更改

<div class="tab_container">

<div class="tab_container" style="height: 516px;">

:或为其提供外部 CSS:

.tabs_container { height: 516px; }

这将防止该一帧的 .tab_content div 消失,从而调整页面大小。

The issue isn't anything with links, though I understand that's the first thought, it's the transition itself.

For a split second (one frame, 13ms to be exact, between the hide and the first frame of the fade in) there is no content in the area the tab panels go, so the page scrolls up because the document was shorter.

To avoid this you need to prevent the document getting any smaller, luckily for your particular page that's pretty easy. Just change this:

<div class="tab_container">

To this:

<div class="tab_container" style="height: 516px;">

Or give it external CSS:

.tabs_container { height: 516px; }

This will prevent the .tab_content divs being gone for that one frame from resizing the page.

等风也等你 2024-09-23 12:35:18

您不能使用 fadeTo 代替 fadeIn。例如:

//Hide all content
$(".tab_content").hide();
//Activate first tab
$(".TabsScheda li:first").addClass("active").show();
//Show first tab content
$(".tab_content:first").show(); 

//On Click Event
$(".TabsScheda li").click(function() {
    //Remove any "active" class
    $(".TabsScheda li").removeClass("active");
    //Add "active" class to selected tab
    $(this).addClass("active");
    //Hide all tab content
    $(".tab_content").fadeTo("slow", 0);
    //Find the href attribute value to identify the active tab + content
    var activeTab = $(this).find("a").attr("href");
    //Fade in the active ID content
    $(activeTab).fadeTo("slow", 1);
    return false;
}

You can't use fadeTo instead of fadeIn. For example:

//Hide all content
$(".tab_content").hide();
//Activate first tab
$(".TabsScheda li:first").addClass("active").show();
//Show first tab content
$(".tab_content:first").show(); 

//On Click Event
$(".TabsScheda li").click(function() {
    //Remove any "active" class
    $(".TabsScheda li").removeClass("active");
    //Add "active" class to selected tab
    $(this).addClass("active");
    //Hide all tab content
    $(".tab_content").fadeTo("slow", 0);
    //Find the href attribute value to identify the active tab + content
    var activeTab = $(this).find("a").attr("href");
    //Fade in the active ID content
    $(activeTab).fadeTo("slow", 1);
    return false;
}

不要返回 false,而是执行 e.preventDefault() (为什么可以在这里找到:http://css-tricks.com/return-false-and-prevent-default/):

$("ul.tabs li").click(function(e) {
    e.preventDefault()
    $("ul.tabs li").removeClass("active"); //Remove any "active" class
    $(this).addClass("active"); //Add "active" class to selected tab
    $(".tab_content").hide();
    var activeTab = $(this).find("a").attr("href");
    $(activeTab).fadeIn('slow', function() {
    $(this).show(); });

});

但问题与 return false 无关,因为未遵循该链接。问题是您隐藏当前选项卡内容,然后淡入新选项卡内容。这会导致您身体的高度发生变化,因此滚动条会变短,因为旧内容被隐藏,使您弹出到顶部。您可以尝试获取 div 的当前高度、将要加载的内容的高度并动态更改 div 的高度。

Instead of returning false, do e.preventDefault() (why can be found here: http://css-tricks.com/return-false-and-prevent-default/):

$("ul.tabs li").click(function(e) {
    e.preventDefault()
    $("ul.tabs li").removeClass("active"); //Remove any "active" class
    $(this).addClass("active"); //Add "active" class to selected tab
    $(".tab_content").hide();
    var activeTab = $(this).find("a").attr("href");
    $(activeTab).fadeIn('slow', function() {
    $(this).show(); });

});

But the problem has nothing to do with the return false, because the link isn't followed. The problem is you hide the current tab content , and then fade in the new one. This results in changing height of your body so the scrollbar is getting shorter because the old content is hidden making you pop to the top. You could try to get the current height of the div, the height of the content that will be loaded and alter the height of the div dynamically.

甜`诱少女 2024-09-23 12:35:18

我认为使用 fadeTo 比通过 CSS 分配高度更干净的解决方案。这样您就不必担心换入或换出的内容的高度。

淡出某些内容并淡入传入函数的数据的示例(例如,作为 ajax 调用的结果):

请注意,您必须使用 fadeTo 淡入内容(而不是仅仅淡入)为了将不透明度恢复为可见的东西。

function swapData(data)
{
  sel = '#tab_container';
  $(sel).fadeTo(50, 0.10, function() { $(sel).html(data).fadeTo(300, 1) } );
}

I think that using fadeTo is much cleaner solution than assigning height via CSS. That way you don't have to worry about the height of the content you are swapping in or out.

An example with a fade out of some content and a fade in of data passed into the function (e.g. as result of an ajax call):

Note that you have to use fadeTo to fade the content back in (as opposed to just fadeIn) in order to return the opacity to something visible.

function swapData(data)
{
  sel = '#tab_container';
  $(sel).fadeTo(50, 0.10, function() { $(sel).html(data).fadeTo(300, 1) } );
}
情丝乱 2024-09-23 12:35:18

另一个有用的提示,尤其是当链接存在 url 时,您可以在脚本完成执行 return false; 时返回 false。这将阻止执行链接的默认操作,即转到 href。对于没有 JS 的浏览器,用户会正常重定向到该 url。

Another useful tip, more so when there is a <noscript> url for a link, you can return false when your script has finished executing return false;. This stops the default action of the link from being executed, i.e. going to the href. For browsers without JS, the user would redirect to the url as normal.

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