简体   繁体   English

使用Javascript设置UL的CSS Left属性

[英]Set CSS Left property of UL using Javascript

So i'm trying to make my schools navigation bar a little bit more mobile friendly. 因此,我正在尝试使学校的导航栏更加移动友好。 If you navigate to www.jpc.wa.edu.au on a mobile device, and click on the menu item "Resources" - A drop down menu opens up with two options. 如果您在移动设备上导航至www.jpc.wa.edu.au,然后单击菜单项“资源”,则将打开一个下拉菜单,其中有两个选项。 Mousing over these with a desktop computer gives you another menu to the right of the first. 将这些鼠标悬停在台式计算机上,可以在第一个菜单的右侧找到另一个菜单。 This works fine on a large screen. 在大屏幕上可以正常工作。 However, anything that has a resolution lower than roughly 1460px, such as most tablets, phones, or even laptops, the new menu gets cut off and you have to pan the window left to view it. 但是,任何分辨率低于大约1460px的东西,例如大多数平板电脑,手机甚至是笔记本电脑,都会中断新菜单,您必须向左平移窗口才能查看它。 I would like to avoid this problem completely by simply positioning the second submenu to the left of the first submenu on smaller screens. 我想通过在较小的屏幕上简单地将第二个子菜单放在第一个子菜单的左侧来完全避免此问题。 Determining screen size I can do. 确定我可以做的屏幕尺寸。

if(window.innerWidth<1460)

Works perfectly fine. 工作完美。 The problem i'm having is that I can't seem to access the second submenus left property to modify the menus position from my javascript 我遇到的问题是,我似乎无法访问第二个子菜单的left属性来从我的JavaScript修改菜单位置

In my CSS, I set it by using the following selector 在我的CSS中,我使用以下选择器进行设置

#horizonMenu

But when I try to use this selector in Javascript using 但是当我尝试在Javascript中使用以下选择器时

document.querySelector("#horizonMenu").style.left

But that doesn't seem to return any values. 但这似乎没有返回任何值。

What am I doing wrong guys? 我在做错人吗? Or should I just take a whole different strategy to fixing this problem? 还是我应该采取完全不同的策略来解决此问题?

Edit: I've now changed horizonMenu to a class. 编辑:现在我已经将horizo​​nMenu更改为一个类。 Alas, my problem seems to persist :( las,我的问题似乎仍然存在:(

style property is the representation of the style attribute. style属性是的代表性style属性。 Since you are setting the CSS property in your CSS file, the left property returns an empty string. 由于要在CSS文件中设置CSS属性,因此left属性将返回一个空字符串。 Also properties of the style object do not show the computed values. style对象的属性也不会显示计算值。 For getting the computed value you should use window.getComputedStyle() function. 为了获得计算值,您应该使用window.getComputedStyle()函数。

window.getComputedStyle(document.querySelector("#horizonMenu"), null)
      .getPropertyValue('left')

For setting the property value you can code: 要设置属性值,您可以编写以下代码:

document.querySelector("#horizonMenu").style.left = '...';

Note that you can consider using CSS media queries instead of using JavaScript. 请注意,您可以考虑使用CSS媒体查询而不是使用JavaScript。

The problem is that you have multiple elements with the ID horizonMenu. 问题是您的ID包含多个元素Horizo​​nMenu。 You should only have a single element with a given ID. 您应该只有一个具有给定ID的元素。 When you query #horizonMenu, it is returning the Community -> Pastoral Care menu, which does not have a left value. 当您查询#horizo​​nMenu时,它返回的是Community-> Pastoral Care菜单,该菜单没有左值。 You should use classes instead, as you can have multiple elements share a class, or change the id to something unique so you can access what you intend to access. 您应该改用类,因为您可以让多个元素共享一个类,或者将id更改为唯一的名称,以便可以访问要访问的内容。

Take a look at the following links 看一下以下链接

Change an element's class with JavaScript and http://www.kirupa.com/html5/setting_css_styles_using_javascript.htm 使用JavaScripthttp://www.kirupa.com/html5/setting_css_styles_using_javascript.htm 更改元素的类

Also please check the position attribute for the elements, see below example: https://jsfiddle.net/masoodalam78/e3erq6j4/ 另外,请检查元素的position属性,请参见以下示例: https : //jsfiddle.net/masoodalam78/e3erq6j4/

var elementObj = document.querySelector("#menu");
elementObj.style.backgroundColor = "blue";
elementObj.left = "200px";

Okay. 好的。 so it may have been a combination of a few things. 因此这可能是几件事的结合。

  1. I followed JBzd's advice to change the menu to a class. 我按照JBzd的建议将菜单更改为一个类。

  2. I also stopped using querySelector() and instead am using querySelectorAll() - storing what's returned as a variable and accessing the correct menus by using varaible[1] and variable[2] 我也停止使用querySelector(),而是使用querySelectorAll()-将返回的内容存储为变量,并使用varaible [1]和variable [2]访问正确的菜单

  3. I am now able to access the left property of the menu by using 现在,我可以使用来访问菜单的left属性

     variable[x].style.left = '162px' 

Thanks for all the help everyone :) 谢谢各位的帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM