Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 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 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...