Javascript 更改打开和关闭导航菜单的方向

Javascript 更改打开和关闭导航菜单的方向,javascript,html,css,Javascript,Html,Css,我有一张表格。在此表单中,当屏幕处于移动大小时,必须使用导航菜单显示表单的详细信息 我的网站是多语言的。当语言为en时,必须为ltr并从left页面打开导航;当语言为fa时,必须为rtl从right打开导航 我写的代码,但我不知道如何改变开放方向的导航。我把我的代码放在这里,请帮我解决这个问题 这是js代码: function openNav(){ document.getElementById("activity-info").style.width = "80%"; } 可

我有一张表格。在此表单中,当屏幕处于移动大小时,必须使用导航菜单显示表单的详细信息

我的网站是多语言的。当语言为
en
时,必须为
ltr
并从
left
页面打开导航;当语言为
fa
时,必须为
rtl
right
打开导航

我写的代码,但我不知道如何改变开放方向的导航。我把我的代码放在这里,请帮我解决这个问题

这是js代码:

    function openNav(){
   document.getElementById("activity-info").style.width = "80%";
}

可以通过在动画开始之前更改div的初始位置来完成此操作

function openNav(){
   document.getElementById("activity-info").style.width = "80%";
   if(language === 'fa') { // Assumed variable name language
      document.getElementById("activity-info").style.right = 0;
      document.getElementById("activity-info").style.left = 'auto';
   } else {
      document.getElementById("activity-info").style.left = 0;
  }
}

可以通过在动画开始之前更改div的初始位置来完成此操作

function openNav(){
   document.getElementById("activity-info").style.width = "80%";
   if(language === 'fa') { // Assumed variable name language
      document.getElementById("activity-info").style.right = 0;
      document.getElementById("activity-info").style.left = 'auto';
   } else {
      document.getElementById("activity-info").style.left = 0;
  }
}

要使其从右侧打开,只需删除
left
属性,然后添加
right
属性,该属性的值与
left
属性的值相同。要从左侧打开它,请使用以下方法:

  .activity-info {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
  }
  .activity-info {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    right: 0; // here is the trick
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
  }
要从右侧打开,请使用以下命令:

  .activity-info {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
  }
  .activity-info {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    right: 0; // here is the trick
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
  }
那么,不重复代码的解决方案是什么呢? 非常简单,您将有1个类(主类)和2个其他类(根据语言从右侧或左侧打开)

这是主要课程:

  .activity-info {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
  }
左类:

  .activity-info-left {
    left: 0;
  }
正确等级:

  .activity-info-right {
    right: 0;
  }
在html中:

<div id="activity-info" class="activity-info activity-info-right">


您已经根据该语言向div添加了相应的类。您可以找到如何将类添加到元素。

要使其从右侧打开,只需删除
left
属性,然后添加
right
属性,该属性的值与
left
中的
属性的值相同。要从左侧打开它,请使用以下命令:

  .activity-info {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
  }
  .activity-info {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    right: 0; // here is the trick
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
  }
要从右侧打开,请使用以下命令:

  .activity-info {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
  }
  .activity-info {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    right: 0; // here is the trick
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
  }
那么,不重复代码的解决方案是什么呢? 非常简单,您将有1个类(主类)和2个其他类(根据语言从右侧或左侧打开)

这是主要课程:

  .activity-info {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
  }
左类:

  .activity-info-left {
    left: 0;
  }
正确等级:

  .activity-info-right {
    right: 0;
  }
在html中:

<div id="activity-info" class="activity-info activity-info-right">

您已经根据该语言向div添加了相应的类。您可以找到如何将类添加到元素中