如何确保我的 jquery 水平手风琴始终占据 100% 宽度?

发布于 2024-12-11 06:15:32 字数 1176 浏览 0 评论 0原文

我正在尝试开发自己的水平手风琴,而无需使用插件。它应该占据整个浏览器窗口的宽度。手风琴部分通过鼠标输入打开。

它或多或少是有效的(前提是该部分在悬停在另一个部分上之前允许完全展开),但是,如果这些部分快速悬停在手风琴的整个宽度上,那么手风琴的整个宽度就会缩小,因为“选定”部分的增长时间比较小的部分。

我需要使手风琴保持 100% 浏览器宽度,但我不确定需要做什么才能实现此目的。

我创建了一个 jsFiddle 来说明(http://jsfiddle.net/AUu9E/),将鼠标滑过他们很快就能看到我正在尝试解决的问题。

非常感谢。

var qtySect = 6;
var windowWidth = $(window).width();
var selectedWidth = windowWidth * 0.7;
var unselectedWidth = windowWidth * 0.3 / (qtySect - 1);
$("nav ul li").fadeTo(0,0.6);
    //set initial widths
$(".unselected").css("width", unselectedWidth);
$(".liHome").css("width",selectedWidth).addClass("selected");
    //mouseover animate
$("nav ul li").mouseenter(function() {
    if ($(this).hasClass("unselected")){
            $(this).animate( { width: selectedWidth }, { queue: false, duration: 1000, easing: "linear" } )
        $(".selected").animate( { width: unselectedWidth }, { queue: false, duration: 1000, easing: "linear" } );
        $(".selected").addClass("unselected").removeClass("selected");
        $(this).addClass("selected").removeClass("unselected");
    }
});

I'm trying to develop my own horizontal accordion without having to use a plugin. It should take up the full browser-window width. The accordion sections open on mouseenter.

It's more-or-less working (providing the section is allowed to expand fully before hovering over another one), however, if the sections are hovered over quickly the entire width of the accordion shrinks as the 'selected' section takes longer to grow than the smaller section.

I need to make the accordion maintain the 100% browser-width, but I'm not sure what I need to be doing to achieve this.

I have created a jsFiddle to illustrate (http://jsfiddle.net/AUu9E/), run the mouse across them quickly to see the problem I'm trying to solve.

Many thanks in advance.

var qtySect = 6;
var windowWidth = $(window).width();
var selectedWidth = windowWidth * 0.7;
var unselectedWidth = windowWidth * 0.3 / (qtySect - 1);
$("nav ul li").fadeTo(0,0.6);
    //set initial widths
$(".unselected").css("width", unselectedWidth);
$(".liHome").css("width",selectedWidth).addClass("selected");
    //mouseover animate
$("nav ul li").mouseenter(function() {
    if ($(this).hasClass("unselected")){
            $(this).animate( { width: selectedWidth }, { queue: false, duration: 1000, easing: "linear" } )
        $(".selected").animate( { width: unselectedWidth }, { queue: false, duration: 1000, easing: "linear" } );
        $(".selected").addClass("unselected").removeClass("selected");
        $(this).addClass("selected").removeClass("unselected");
    }
});

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

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

发布评论

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

评论(1

心如荒岛 2024-12-18 06:15:32

嗯,你不能用百分比来代替吗?似乎消除了大量代码:)

似乎您需要使用“STEP”过程,以确保它们相互遵循。
可以在这里找到这样的示例 http://www.protofunc.com/scripts/jquery /animate-step/

http://jsfiddle.net/AUu9E/3/

HTML

<nav>
            <ul>
                <li class="liHome">home</li>
                <li class="liRsvp">rsvp</li>
                <li class="liStory">story</li>
                <li class="liVenue">venue</li>
                <li class="liGuestbook">guestbook</li>
                <li class="liGallery">gallery</li>
            </ul>
</nav>

JS

//Remove 1 element(selected), and find the width of the rest(unselected)
var qtySect = 6-1;
var unselectedWidth = 40 / qtySect +"%";
var selectedWidth = 60+"%";

    //set initial widths
$("nav ul li:first-child").addClass('selected');
$("nav ul li").not(':first-child').addClass('unselected');
$(".unselected").css("width", unselectedWidth);
$(".selected").css("width",selectedWidth);
    //mouseover animate
$("nav ul li").mouseenter(function() {
    if ($(this).hasClass("unselected")){
         $(this).animate( { width: selectedWidth }, { queue: false, duration: 1000, easing: "linear" } )
         $(".selected").animate( { width: unselectedWidth }, { queue: false, duration: 1000, easing: "linear" } );
        $(".selected").addClass("unselected").removeClass("selected");
        $(this).addClass("selected").removeClass("unselected");
    }
});

Hmmm, couldn't you use percentages instead? Seems to eliminate alot of code :)

It seems you need to use a "STEP" proccess, to make sure they follow each other.
Example of such can be found here http://www.protofunc.com/scripts/jquery/animate-step/

http://jsfiddle.net/AUu9E/3/

HTML

<nav>
            <ul>
                <li class="liHome">home</li>
                <li class="liRsvp">rsvp</li>
                <li class="liStory">story</li>
                <li class="liVenue">venue</li>
                <li class="liGuestbook">guestbook</li>
                <li class="liGallery">gallery</li>
            </ul>
</nav>

JS

//Remove 1 element(selected), and find the width of the rest(unselected)
var qtySect = 6-1;
var unselectedWidth = 40 / qtySect +"%";
var selectedWidth = 60+"%";

    //set initial widths
$("nav ul li:first-child").addClass('selected');
$("nav ul li").not(':first-child').addClass('unselected');
$(".unselected").css("width", unselectedWidth);
$(".selected").css("width",selectedWidth);
    //mouseover animate
$("nav ul li").mouseenter(function() {
    if ($(this).hasClass("unselected")){
         $(this).animate( { width: selectedWidth }, { queue: false, duration: 1000, easing: "linear" } )
         $(".selected").animate( { width: unselectedWidth }, { queue: false, duration: 1000, easing: "linear" } );
        $(".selected").addClass("unselected").removeClass("selected");
        $(this).addClass("selected").removeClass("unselected");
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文