简体   繁体   English

将元素移到左侧javascript / css

[英]Move element to left javascript/css

I'm co-building a website where we want to use a off-canvas menu for the lesser used menu options. 我正在共同建设一个网站 ,在该网站上我们希望使用画布菜单作为较少使用的菜单选项。 We used this script to add a nice effect to the hamburger menu; 我们使用此脚本在汉堡菜单中添加了不错的效果。 works well! 效果很好! But we have a problem, the off-canvas menu slides over the button when clicked so we want the button to move to the left when clicked. 但是我们有一个问题,单击时“画布”菜单会滑过按钮,因此我们希望单击时将按钮移到左侧。 Unfortunately targeting the .is-active class with a margin-right: 600px doesn't work. 不幸的是,将.is-active类margin-right: 600px无效。 Do you guys have any idea? 你们有什么主意吗? Note: The button that get's slide over is in the navbar (top-right) the button called 'hamburger menu' is temporary to work on the menu while the other button is hidden. 注意:滑过的按钮在导航栏中(右上角),名为“汉堡菜单”的按钮是临时可在菜单上使用,而其他按钮则被隐藏。


The button itself uses the following js in which $("#wrapper").toggleClass("toggled"); 该按钮本身使用以下js,其中$("#wrapper").toggleClass("toggled"); makes the off-canvas menu show: 使画布外菜单显示:

(function() {

"use strict";

var toggles = document.querySelectorAll(".c-hamburger");

for (var i = toggles.length - 1; i >= 0; i--) {
  var toggle = toggles[i];
  toggleHandler(toggle);
};

function toggleHandler(toggle) {
  toggle.addEventListener( "click", function(e) {
    e.preventDefault();
    $("#wrapper").toggleClass("toggled");
    (this.classList.contains("is-active") === true) ? this.classList.remove("is-active") : this.classList.add("is-active");

  });
}

 })();

Which runs after clicking on the button: <button class="c-hamburger c-hamburger--htx pull-right"><span>toggle menu</span></button> 单击按钮后运行: <button class="c-hamburger c-hamburger--htx pull-right"><span>toggle menu</span></button>

For a quick and dirty temporary solution, you could make the hamburger icon appear on top of the menu by applying the below css to the hamburger icon 为了获得快速而又肮脏的临时解决方案,您可以通过将以下CSS应用于汉堡包图标,使汉堡包图标显示在菜单顶部

z-index: 2000

For a longer term solution, one way would be to add javascript to animate the icon over when the menu is toggled. 对于较长期的解决方案,一种方法是在切换菜单时添加JavaScript来对图标进行动画处理。 You could use the jquery animate() command: 您可以使用jquery animate()命令:

if (this.classList.contains("is-active") === true) {
    $('.c-hamburger').animate({right: '0px'});
else {
    $('.c-hamburger').animate({right: '200px'});
}

If you add that to the end of your function toggleHandler(toggle) above it should work 如果将其添加到上面的函数toggleHandler(toggle)的末尾,它应该可以工作

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

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