Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
iOS Range Toggle Sliders
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Markshall" data-source="post: 445412" data-attributes="member: 1872"><p>I <a href="https://codepen.io/marcusconnor/post/checkbox-toggle-switches-are-confusing-ui" target="_blank"><strong>saw a post</strong></a> on Codepen and wanted to see if I could replicate the famous toggle-sliders we're all familiar with that use cleverly-styled checkboxes, instead of using range sliders like Marcus suggests for UI purposes.</p><p></p><p><strong>TL;DR of the Codepen post:</strong> author wants to be able to actually drag the toggle-button rather than just click it</p><p></p><p><a href="https://codepen.io/Markshall/pen/XOQJNB" target="_blank">https://codepen.io/Markshall/pen/XOQJNB</a></p><p></p><p>HTML</p><p>[CODE=html]<div class="range-toggles"></p><p> <input type="range" class="range-toggle" value="0" min="0" max="1" /></p><p> <input type="range" class="range-toggle" value="0" min="0" max="1" /></p><p> <input type="range" class="range-toggle" value="1" min="0" max="1" /></p><p> <input type="range" class="range-toggle" value="0" min="0" max="1" /></p><p> <input type="range" class="range-toggle" value="1" min="0" max="1" /></p><p></div>[/CODE]</p><p></p><p>JavaScript</p><p>[CODE=javascript]document.querySelectorAll('.range-toggle').forEach(function(el, i) {</p><p> if (el.value === '1') el.classList.add('active'); //for sliders that are value=1 on page load</p><p></p><p> el.addEventListener('mousedown', function(e) {</p><p> var hasDragged = !1,</p><p> initVal = el.value;</p><p></p><p> el.onmousemove = function() {hasDragged = 1;}</p><p></p><p> el.onmouseup = function(e) {</p><p> !hasDragged && (el.value = el.value==='1' ? '0' : '1');</p><p></p><p> (initVal != el.value) && el.classList.toggle('active');</p><p> hasDragged = !1;</p><p> }</p><p> });</p><p>});[/CODE]</p><p></p><p>SCSS</p><p>[CODE=scss]</p><p>@mixin range-thumb() {</p><p> -webkit-appearance: none;</p><p> appearance: none;</p><p> width: 25px;</p><p> height: 25px;</p><p> background-color: #fff;</p><p> border-radius: 25px;</p><p> box-shadow: 0 2px 6px 2px rgba(50, 50, 50, .2);</p><p> cursor: pointer;</p><p> pointer-events: all;</p><p> transition: width .15s ease-in-out;</p><p>}</p><p></p><p>.range-toggle {</p><p> -webkit-appearance: none;</p><p> appearance: none;</p><p> width: 48px;</p><p> height: 25px;</p><p> outline: none;</p><p> background-color: #fff;</p><p> border: 2px solid #e6e6e6;</p><p> border-radius: 25px;</p><p> transition: all .3s cubic-bezier(0.61, -0.1, 0, 1.49);</p><p> pointer-events: none;</p><p></p><p> &.active {</p><p> background-color: #4dde5e;</p><p> border-color: #4dde5e;</p><p> }</p><p></p><p> &::-webkit-slider-thumb {</p><p> @include range-thumb();</p><p> }</p><p></p><p> &:active::-webkit-slider-thumb {</p><p> width: 30px;</p><p> }</p><p></p><p> &::-moz-range-thumb {</p><p> @include range-thumb();</p><p> }</p><p></p><p> &:active::-moz-range-thumb {</p><p> width: 30px;</p><p> }</p><p>}</p><p></p><p>/* helper styles */</p><p>body {</p><p> margin: 0;</p><p> padding: 0;</p><p> height: 100vh;</p><p> display: flex;</p><p> flex-direction: column;</p><p> justify-content: center;</p><p> align-items: center;</p><p> font: 400 15px/1.6 "Helvetica Neue", Helvetica, Arial, sans-serif;</p><p>}</p><p></p><p>p {</p><p> flex: none;</p><p> max-width: 700px;</p><p> text-align: center;</p><p>}</p><p></p><p>.range-toggles {</p><p> display: flex;</p><p>}</p><p>[/CODE]</p><p>[automerge]1550750041[/automerge]</p><p>Updated pen and post to reflect better post. Shouldn't really be any bugs now.</p><p>[automerge]1551262831[/automerge]</p><p>Updated again to behave more like iOS toggles; removed the grey border on 'active' state, toggle width on click/mousedown</p></blockquote><p></p>
[QUOTE="Markshall, post: 445412, member: 1872"] I [URL='https://codepen.io/marcusconnor/post/checkbox-toggle-switches-are-confusing-ui'][B]saw a post[/B][/URL] on Codepen and wanted to see if I could replicate the famous toggle-sliders we're all familiar with that use cleverly-styled checkboxes, instead of using range sliders like Marcus suggests for UI purposes. [B]TL;DR of the Codepen post:[/B] author wants to be able to actually drag the toggle-button rather than just click it [URL]https://codepen.io/Markshall/pen/XOQJNB[/URL] HTML [CODE=html]<div class="range-toggles"> <input type="range" class="range-toggle" value="0" min="0" max="1" /> <input type="range" class="range-toggle" value="0" min="0" max="1" /> <input type="range" class="range-toggle" value="1" min="0" max="1" /> <input type="range" class="range-toggle" value="0" min="0" max="1" /> <input type="range" class="range-toggle" value="1" min="0" max="1" /> </div>[/CODE] JavaScript [CODE=javascript]document.querySelectorAll('.range-toggle').forEach(function(el, i) { if (el.value === '1') el.classList.add('active'); //for sliders that are value=1 on page load el.addEventListener('mousedown', function(e) { var hasDragged = !1, initVal = el.value; el.onmousemove = function() {hasDragged = 1;} el.onmouseup = function(e) { !hasDragged && (el.value = el.value==='1' ? '0' : '1'); (initVal != el.value) && el.classList.toggle('active'); hasDragged = !1; } }); });[/CODE] SCSS [CODE=scss] @mixin range-thumb() { -webkit-appearance: none; appearance: none; width: 25px; height: 25px; background-color: #fff; border-radius: 25px; box-shadow: 0 2px 6px 2px rgba(50, 50, 50, .2); cursor: pointer; pointer-events: all; transition: width .15s ease-in-out; } .range-toggle { -webkit-appearance: none; appearance: none; width: 48px; height: 25px; outline: none; background-color: #fff; border: 2px solid #e6e6e6; border-radius: 25px; transition: all .3s cubic-bezier(0.61, -0.1, 0, 1.49); pointer-events: none; &.active { background-color: #4dde5e; border-color: #4dde5e; } &::-webkit-slider-thumb { @include range-thumb(); } &:active::-webkit-slider-thumb { width: 30px; } &::-moz-range-thumb { @include range-thumb(); } &:active::-moz-range-thumb { width: 30px; } } /* helper styles */ body { margin: 0; padding: 0; height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; font: 400 15px/1.6 "Helvetica Neue", Helvetica, Arial, sans-serif; } p { flex: none; max-width: 700px; text-align: center; } .range-toggles { display: flex; } [/CODE] [automerge]1550750041[/automerge] Updated pen and post to reflect better post. Shouldn't really be any bugs now. [automerge]1551262831[/automerge] Updated again to behave more like iOS toggles; removed the grey border on 'active' state, toggle width on click/mousedown [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
iOS Range Toggle Sliders
Top