即使在移动设备上,我也试图将侧边导航栏向左对齐,目前它在移动设备尺寸(小于 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/