【问题标题】:Is there a way to style checkbox appearance in p5js to become a toggle switch?有没有办法将 p5js 中的复选框外观设置为切换开关?
【发布时间】:2022-01-11 10:30:06
【问题描述】:

我正在尝试使用 javascript 更改 p5 画布中复选框的外观,使其具有与 this 等切换开关相同的外观

我已经尝试在 this sketch 中的 css 文件中进行试验,但它似乎不起作用

任何帮助将不胜感激。谢谢

let checkbox;

function setup() {
  checkbox = createCheckbox("label", false);
  checkbox.changed(myCheckedEvent);

  createDiv(`
    <label class="switch">
      <input id="toggle" type="checkbox"  />
      <span class="slider round"></span>
    </label>`);

  checkbox = select('#toggle');
}

function myCheckedEvent() {
  if (this.checked()) {
    console.log("Checking!");
  } else {
    console.log("Unchecking!");
  }
}
html, body {
  margin: 0;
  padding: 0;
}
canvas {
  display: block;
}

/* The switch - the box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

/* Hide default HTML checkbox */
.siwtch input {
  opacity: 0;
  width: 0;
  height: 0;
}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

#toggle:checked + .slider {
  background-color: #2196F3;
}

#toggle:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

#toggle:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"&gt;&lt;/script&gt;

【问题讨论】:

  • 请将您的代码放入您的问题中并描述什么不起作用。当我运行你的代码时,它显示了一个切换的开关。

标签: javascript css checkbox toggle p5.js


【解决方案1】:

你的方法基本上看起来很合理,它只需要一些调整(见 cmets):

let checkbox;

function setup() {
  noCanvas();
  // There's no need to use createCheckbox() since you're create the <input /> tag yourself
  // Also I got rid of the wrapping div since it wasn't strictly necessary.
  let label = createElement(
    'label',
    `<input id="toggle" type="checkbox" />
     <span class="slider round"></span>`
  );
  
  // Because I'm using createElement() to create the label I have to add the class attribute:
  label.addClass('switch');

  checkbox = select('#toggle');
  // Register the event handler after using select() to get the p5.Element for the checkbox.
  checkbox.changed(myCheckedEvent);
}

function myCheckedEvent() {
  if (this.checked()) {
    console.log("Checking!");
  } else {
    console.log("Unchecking!");
  }
}
html, body {
  margin: 0;
  padding: 0;
}

/* The switch - the box around the slider */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

/* Hide default HTML checkbox */
/* COMMENT: there was a typo here, uses to be .siwtch */
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

#toggle:checked + .slider {
  background-color: #2196F3;
}

#toggle:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

#toggle:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"&gt;&lt;/script&gt;

【讨论】:

  • 我已经实现了你的建议,它在 p5.js 中运行良好,但是当我使用 Atom 在我的项目中运行它时,为什么它似乎不起作用?
  • 不看整个项目就无法说出口。我猜很可能是一些冲突的 CSS 影响了这个标签/输入/跨度的显示方式。我建议使用 Web 浏览器的开发人员工具来查看元素并查看计算的 CSS 属性。那里可能存在一些差异,然后您可以将冲突设置追溯到我们项目中的源。
猜你喜欢
  • 2010-11-24
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多