javascript - 将切换的 div 保持在原位

标签 javascript jquery html css

我正在使用下面的代码将一个 div 向左和向后切换。我有两个问题:

  1. 我只能将 div 切换到最左边的位置一次。有一次,我 已经切换回增益,切换不会再次将 div 移动到它的位置 最左边的位置。

  2. 我正在使用 'right': window.innerWidth - 80 来计算 div 左边的位置。一旦 div 向左移动,我需要 'right': window.innerWidth - 80 不继续重新计算 div 位置。 每次向左切换时,div 都需要保持在原位。

$(document).ready(function() {
  var is_Clicked = false;
  $("#togglebutton").click(function() {
    if (is_Clicked) {
      $('#myToggleDiv').css('float', 'left');
      $("#myToggleDiv").animate({
        left: '0%'
      });
      is_Clicked = false;
    } else {
      $('#myToggleDiv').css('float', 'right');
      $("#myToggleDiv").animate({
        'right': window.innerWidth - 80
      });
      is_Clicked = true;
    }
  });
});

$(document).ready(function() {
  document.getElementById("myText").innerHTML = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br><br>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
});
html,
body {
  width: 100%;
  height: 100%;
}
#header {
  width: 100%;
  height: 20%;
  float: left;
  border: 1px solid;
}
#myToggleDiv {
  position: absolute;
  border: 1px solid;
}
#togglebutton {
  width: 10%;
  height: 40%;
  margin-right: 5px;
  margin-top: 5px;
  float: right;
}
@media screen and (min-width: 1366px) {
  #myToggleDiv {
    width: 60vw;
  }
}
@media screen and (max-width: 1365px) {
  #myToggleDiv {
    width: 70vw;
  }
}
@media screen and (max-width: 1024px) {
  #myToggleDiv {
    width: 90vw;
  }
}
@media screen and (max-width: 800px) {
  #myToggleDiv {
    width: 100vw;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myToggleDiv">
  <input type="button" value="Toggle" id="togglebutton">
  <p id="myText"></p>
</div>

最佳答案

我使用了 css 动画和百分比计算,因此无论窗口大小如何调整,容器始终位于容器左侧 80px。

$(document).ready(function() {
  $("#togglebutton").click(function() {
    var $container = $('#myToggleDiv');
    $container.toggleClass('hide');
  });
});

$(document).ready(function() {
  document.getElementById("myText").innerHTML = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.<br><br>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
});
html,
body {
  width: 100%;
  height: 100%;
}
#header {
  width: 100%;
  height: 20%;
  float: left;
  border: 1px solid;
}
.hide {
  transform: translate(calc(-100% + 80px), 0);
}
#myToggleDiv {
  position: absolute;
  border: 1px solid;
  transition: transform .3s;
}
#togglebutton {
  width: 10%;
  height: 40%;
  margin-right: 5px;
  margin-top: 5px;
  float: right;
}
@media screen and (min-width: 1366px) {
  #myToggleDiv {
    width: 60vw;
  }
}
@media screen and (max-width: 1365px) {
  #myToggleDiv {
    width: 70vw;
  }
}
@media screen and (max-width: 1024px) {
  #myToggleDiv {
    width: 90vw;
  }
}
@media screen and (max-width: 800px) {
  #myToggleDiv {
    width: 100vw;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myToggleDiv">
  <input type="button" value="Toggle" id="togglebutton">
  <p id="myText"></p>
</div>

关于javascript - 将切换的 div 保持在原位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39849785/

相关文章:

javascript - jquery :selected not working as expected

javascript - 图片在 iframe 中加载后未从缓存中加载

javascript - 动态自定义组件上的 V-validate

javascript - 将 jquery 函数连接到 bootstrap 的轮播幻灯片事件

jquery - 哪种方式最好使用 IMG 标签和 CSS 背景图像进行游戏

html - 如何确保 CSS 在所有浏览器上都能正常工作,而无需在每个浏览器上进行眼睛测试?

javascript - 鼠标离开浏览器功能不起作用

javascript - 在 Rails 3 Controller 中处理 jsonp

jQuery fn 命名空间

php - 在选择更改时使用数据库数据填充输入