Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

javascript - Responsive menu with resizing

I need to responsive button like this: we have 15 buttons on a menu. When the browser is resizing, some buttons add to <select> like this: enter image description here

I have this jsFiddle to demonstrate the problem:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use $(window).on('resize', function() { ... }); to detect ant change in width and act accordingly.

Here's a jQuery code that works

$(function() {

$("<select />").appendTo($("nav"));

var $select = $('nav select');
$select.hide();

$("<option />", {
    "selected": "selected",
    "value"   : "",
    "text"    : "Go to..."
}).appendTo($select);

$("nav a").each(function() {
    var el = $(this);
    $("<option />", {
        "value"   : el.attr("href"),
        "text"    : el.text()
    }).appendTo($select);
});

$(window).on('resize', function() {
    console.log($(window).width());
    if($(window).width() < 960) {
        $($select).show();
        $('nav ul').hide();
    }
    else if($(window).width() > 960) {
        $($select).hide();
        $('nav ul').show();
    }
});    

$select.change(function() {
    window.location = $(this).find("option:selected").val();
});
});

Code. See demo here: Demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...