javascript - 我怎样才能让一个按钮在相反的方向做多项事情
问题描述
barsButton.onclick = function xtranslate(){
aside[0].style.transform = "translateX(0px)"
stories.style.marginLeft = "0px";
}
如何单击按钮两次,以便下次将 x 转换为 250px?
解决方案
您可以使用变量并使用条件(三元)运算符更新其值:
var toTranslate = '0px';
barsButton.onclick = function xtranslate(){
aside[0].style.transform = `translateX(${toTranslate})`;
stories.style.marginLeft = "0px";
toTranslate = toTranslate == '0px' ? '250px' : '0px';
}
推荐阅读
- c# - Unity 3D - 通过单击鼠标摧毁游戏对象
- django - 防止在 django 中编辑其他用户的个人资料
- python - 如何查找字符串中子字符串的出现次数并将其存储到 Python 字典中?
- python - 创建将字典中的 2 个键映射到相同值的数据框列
- python - 从列表比较和绘图中获取位置数据
- c++ - 我在打开我创建的文本文件时遇到问题(
) - c++ - 检测到文本数字太大而无法快速转换为 Int
- asynchronous - 有没有办法创建一个异步流生成器来产生重复调用函数的结果?
- ruby-on-rails - 如何修复 ArgumentError:参数数量错误(给定 0,预期为 1) - RoR
- ios - 可访问性:专注于视图