javascript - 我不明白以下javascript

标签 javascript jquery html css jquery-mobile

即使在移动设备上,我也试图将侧边导航栏向左对齐,目前它在移动设备尺寸(小于 768 像素)时仅显示在顶部导航栏下方。我已经尝试了我所知道的一切,所以我认为问题一定出在我的 JavaScript 中,因为这不是我的强项之一。我能理解一点,因为我懂Java。所以我想知道下面的代码做了什么......

$(function() {
    function responsiveView() {
        var wSize = $(window).width();
        if (wSize <= 768) {
            $('#container').addClass('sidebar-close');
            $('#sidebar > ul').hide();
        }

        if (wSize > 768) {
            $('#container').removeClass('sidebar-close');
            $('#sidebar > ul').show();
        }
    $(window).on('load', responsiveView);
    $(window).on('resize', responsiveView);
});

最佳答案

在 java 结构方面将类似于此。但它使用的是 jquery。

Note: alot of the functions are lying in the jquery library so I won't show them below. $ = jquery

public Class $
{
    private void responsiveView()
    {
        int wSize = $.window().width();
        if(wSize <= 768) 
        {
            window.container.addClass('sidebar-close');//it retrieves a reference to the element and adds the className used by CSS
            $('#sidebar > ul').hide();//replaces each ul in the element with the id of sidebar. sorry didn't want to think about the java equivalent
        }

        if (wSize > 768) 
        {
            window.container.removeClass('sidebar-close');
            $('#sidebar > ul').show();//shows all ul within element with id of sidebar
        }
//this codes runs after function/methods are compiled
    $.window.on('load', responsiveView);//sets the function/method to run on window load event
    $.window.on('resize', responsiveView);//sets the function/method to run on window's resize event
    }
}

希望对您有所帮助。 请记住 addclass 将允许元素具有多个类。所以如果你的 css 被分配给类,它将应用两种样式

关于javascript - 我不明白以下javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30299162/

相关文章:

jquery - 使用 jQuery 和 ems 计算 CSS 值

html - 如何使水平滚动获得默认的div宽度?

html - 图片之间有多余的空白吗?

javascript - 使用三个 js 加载 3d 模型示例

javascript - 复选框选择模型 - 检查列标题文本

javascript - 如何为 Canvas 中的现有数据 URI 设置填充样式(纹理)

javascript - 有没有办法同步 gif 文件?

javascript - GetElementByClass 另一个元素中的元素

javascript - 在数据表上填充数据不起作用

html - 带有填充溢出容器的 Flex 元素