From 3424f386fffb242690889a8951b55f6a9437432a Mon Sep 17 00:00:00 2001 From: Becky Gilbert Date: Wed, 28 Oct 2020 12:05:59 -0700 Subject: [PATCH] fix slider label positioning with custom track/thumb CSS & calc - fixes #695 --- css/jspsych.css | 73 ++++++++++++++++++++++++ plugins/jspsych-audio-slider-response.js | 20 +++++-- plugins/jspsych-html-slider-response.js | 16 ++++-- plugins/jspsych-image-slider-response.js | 22 ++++--- plugins/jspsych-video-slider-response.js | 22 ++++--- 5 files changed, 129 insertions(+), 24 deletions(-) diff --git a/css/jspsych.css b/css/jspsych.css index 20d10905..9a07da4d 100644 --- a/css/jspsych.css +++ b/css/jspsych.css @@ -96,6 +96,79 @@ cursor: not-allowed; } +/* custom style for input[type="range] (slider) to improve alignment between positions and labels */ + +.jspsych-slider { + appearance: none; + -webkit-appearance: none; + -moz-appearance: none; + width: 100%; + background: transparent; +} +.jspsych-slider:focus { + outline: none; +} +/* track */ +.jspsych-slider::-webkit-slider-runnable-track { + appearance: none; + -webkit-appearance: none; + width: 100%; + height: 8px; + cursor: pointer; + background: #eee; + box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d; + border-radius: 2px; + border: 1px solid #aaa; +} +.jspsych-slider::-moz-range-track { + appearance: none; + width: 100%; + height: 8px; + cursor: pointer; + background: #eee; + box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d; + border-radius: 2px; + border: 1px solid #aaa; +} +.jspsych-slider::-ms-track { + appearance: none; + width: 99%; + height: 14px; + cursor: pointer; + background: #eee; + box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d; + border-radius: 2px; + border: 1px solid #aaa; +} +/* thumb */ +.jspsych-slider::-webkit-slider-thumb { + border: 1px solid #666; + height: 24px; + width: 15px; + border-radius: 5px; + background: #ffffff; + cursor: pointer; + -webkit-appearance: none; + margin-top: -9px; +} +.jspsych-slider::-moz-range-thumb { + border: 1px solid #666; + height: 24px; + width: 15px; + border-radius: 5px; + background: #ffffff; + cursor: pointer; +} +.jspsych-slider::-ms-thumb { + border: 1px solid #666; + height: 20px; + width: 15px; + border-radius: 5px; + background: #ffffff; + cursor: pointer; + margin-top: -2px; +} + /* jsPsych progress bar */ #jspsych-progressbar-container { diff --git a/plugins/jspsych-audio-slider-response.js b/plugins/jspsych-audio-slider-response.js index 9b37b92d..7f909204 100644 --- a/plugins/jspsych-audio-slider-response.js +++ b/plugins/jspsych-audio-slider-response.js @@ -86,6 +86,9 @@ jsPsych.plugins['audio-slider-response'] = (function() { plugin.trial = function(display_element, trial) { + // half of the thumb width value from jspsych.css, used to adjust the label positions + var half_thumb_width = 7.5; + // setup stimulus var context = jsPsych.pluginAPI.audioContext(); if(context !== null){ @@ -109,17 +112,22 @@ jsPsych.plugins['audio-slider-response'] = (function() { } var html = '
'; - html += '
'; - html += ''; + html += ''; html += '
' for(var j=0; j < trial.labels.length; j++){ - var width = 100/(trial.labels.length-1); - var left_offset = (j * (100 /(trial.labels.length - 1))) - (width/2); - html += '
'; + var label_width_perc = 100/(trial.labels.length-1); + var percent_of_range = j * (100/(trial.labels.length - 1)); + var percent_dist_from_center = ((percent_of_range-50)/50)*100; + var offset = (percent_dist_from_center * half_thumb_width)/100; + html += '
'; html += ''+trial.labels[j]+''; html += '
' } diff --git a/plugins/jspsych-html-slider-response.js b/plugins/jspsych-html-slider-response.js index 1e38408d..d59309de 100644 --- a/plugins/jspsych-html-slider-response.js +++ b/plugins/jspsych-html-slider-response.js @@ -102,19 +102,27 @@ jsPsych.plugins['html-slider-response'] = (function() { plugin.trial = function(display_element, trial) { + // half of the thumb width value from jspsych.css, used to adjust the label positions + var half_thumb_width = 7.5; + var html = '
'; html += '
' + trial.stimulus + '
'; html += '
'; - html += ''; + html += ''; html += '
' for(var j=0; j < trial.labels.length; j++){ - var width = 100/(trial.labels.length-1); - var left_offset = (j * (100 /(trial.labels.length - 1))) - (width/2); - html += '
'; + var label_width_perc = 100/(trial.labels.length-1); + var percent_of_range = j * (100/(trial.labels.length - 1)); + var percent_dist_from_center = ((percent_of_range-50)/50)*100; + var offset = (percent_dist_from_center * half_thumb_width)/100; + html += '
'; html += ''+trial.labels[j]+''; html += '
' } diff --git a/plugins/jspsych-image-slider-response.js b/plugins/jspsych-image-slider-response.js index 4448102d..74bcac74 100644 --- a/plugins/jspsych-image-slider-response.js +++ b/plugins/jspsych-image-slider-response.js @@ -122,6 +122,9 @@ jsPsych.plugins['image-slider-response'] = (function() { plugin.trial = function(display_element, trial) { + // half of the thumb width value from jspsych.css, used to adjust the label positions + var half_thumb_width = 7.5; + var html = '
'; html += '
'; html += ''; html += '
'; - html += '
'; - html += ''; + html += ''; html += '
' for(var j=0; j < trial.labels.length; j++){ - var width = 100/(trial.labels.length-1); - var left_offset = (j * (100 /(trial.labels.length - 1))) - (width/2); - html += '
'; + var label_width_perc = 100/(trial.labels.length-1); + var percent_of_range = j * (100/(trial.labels.length - 1)); + var percent_dist_from_center = ((percent_of_range-50)/50)*100; + var offset = (percent_dist_from_center * half_thumb_width)/100; + html += '
'; html += ''+trial.labels[j]+''; html += '
' } diff --git a/plugins/jspsych-video-slider-response.js b/plugins/jspsych-video-slider-response.js index bc261c13..1260b5bd 100644 --- a/plugins/jspsych-video-slider-response.js +++ b/plugins/jspsych-video-slider-response.js @@ -145,6 +145,9 @@ jsPsych.plugins["video-slider-response"] = (function() { plugin.trial = function(display_element, trial) { + // half of the thumb width value from jspsych.css, used to adjust the label positions + var half_thumb_width = 7.5; + // setup stimulus var video_html = '