【问题标题】:Best practise to realize up down arrows in select field在选择字段中实现向上向下箭头的最佳实践
【发布时间】:2016-12-06 14:27:57
【问题描述】:

在网页的选择元素中设置百分比向上/向下滑块/选择器的最佳方法是:

  1. 具有专用背景的选择列表。如果是,如何检测到用户点击了向上或向下箭头?
  2. 为每个百分比向上/向下项目构建一个包含三行/一列的表格,其中:a) 第 1 行 = 向上箭头,b) 第 2 行 = 选择选项列表,c) 第 3 行 = 向下箭头
  3. 其他解决方案?

备注:百分比可按5%递增/递减

【问题讨论】:

  • 点击箭头的预期结果是否只是增加或减少值?
  • 显示你的尝试
  • @guest271314:点击箭头应该增加/减少值,是的。此外,onchange 事件应启用对 javascript 函数的调用。
  • @jafarbtech:到目前为止我还没有尝试任何东西,因为我想知道在这种情况下的最佳实践。
  • 在我上传到 youtube 的 3 秒视频中,如果我点击向上箭头 appr.每秒4次,有选择效果。截图:i.stack.imgur.com/TTRVY.png

标签: javascript jquery html css


【解决方案1】:

您可以使用<input type="number"> 元素、<label> 元素、css :before:afterinput 事件来处理对input type="number" 元素的点击,自定义事件来处理对label 元素的点击.

$("label[for]").on("click", function(event) {
  $("#input").val(function(_, n) {
    return event.target.htmlFor === "up" 
           ? +n < +this.max ? +n + 5 : n 
           : +n > +this.min ? +n - 5 : n;
  }).trigger("arrow")
});

$("#input").on("input arrow", function(event) {
  if (event.isTrigger) {
    console.log("arrow");
  } else {
    console.log("input")
  }
  console.log(this.value + "%")
});
@charset "UTF-8";
 div {
  position: relative;
  top: 50px;
}
input[type="number"] {
  width: 45px;
  outline: thin solid navy;
}
label[for="up"]:nth-of-type(1):before {
  content: "\25B2";
  top: -20px !important;
  left: 32.5px;
  position: relative;
}
label[for="down"]:before {
  content: "\25BC";
  position: relative;
  top: 20px !important;
  left: -32.5px;
}
label[for="down"]:after {
  content: "%";
  position: relative;
  font-weight: bold;
  font-size: 14px;
  position: relative;
  left: -10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <select id="select1">
    <option value="abc">abc</option>
  </select>
  <label for="up"></label><input id="input" type="number" min="0" max="100" step="5" value="0"><label for="down"></label>
</div>
<script>
</script>

【讨论】:

  • 只剩下一个问题:在 Chrome (Ubuntu) 中快速单击会奇怪地选择 Web 内容。这不会出现在 Firefox@Ubuntu 中。见:[链接]youtube.com/watch?v=rntq0o0nFc0
  • @user2145488 不确定要传达什么视频?
  • 视频只是显示了我提到的行为:如果我快速单击“向上”箭头,则可以看到选择。
  • “选择可见”是什么意思?
  • 在我上传到 youtube 的 3 秒视频中,如果我点击向上箭头 appr.每秒 4 次,每秒钟或第三次单击 html 元素的几个部分(如“abc”、百分比值本身等)。见截图:[链接]i.stack.imgur.com/TTRVY.png
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多