如何在jsPsych的canvas-slider-response插件中隐藏滑块thumb直至参与者响应
核心调整思路
- 原有CSS中webkit内核的滑块thumb可见性属性被注释,且没有做显示状态的样式适配
- 事件监听逻辑中选择的滑块ID对应错误,且
visibility属性的有效值为visible而非show
步骤1:更新CSS样式
/* 默认隐藏所有浏览器的滑块thumb */ .jspsych-slider::-webkit-slider-thumb { -webkit-appearance: none; visibility: hidden; height: 24px; width: 15px; border-radius: 5px; background: #ffffff; border: 1px solid #666; cursor: pointer; margin-top: -9px; } .jspsych-slider::-moz-range-thumb { visibility: hidden; height: 24px; width: 15px; border-radius: 5px; background: #ffffff; border: 1px solid #666; cursor: pointer; } .jspsych-slider::-ms-thumb { visibility: hidden; height: 24px; width: 15px; border-radius: 5px; background: #ffffff; border: 1px solid #666; cursor: pointer; } /* 匹配显示状态的样式 */ .jspsych-slider.slider-thumb-visible::-webkit-slider-thumb { visibility: visible; } .jspsych-slider.slider-thumb-visible::-moz-range-thumb { visibility: visible; } .jspsych-slider.slider-thumb-visible::-ms-thumb { visibility: visible; }
如果样式不生效,可以在visibility属性值后加!important提升优先级。
步骤2:调整对应trial的事件监听逻辑
canvas-slider-response插件的滑块ID为#jspsych-canvas-slider-response-response,替换原有监听逻辑即可:
on_load: function() { const slider = document.querySelector('#jspsych-canvas-slider-response-response'); const showThumb = function(e) { slider.classList.add('slider-thumb-visible'); // 可选:让thumb直接定位到点击位置 slider.value = Math.round((e.offsetX / slider.offsetWidth) * (slider.max - slider.min)); // 移除监听避免重复触发 slider.removeEventListener('mousedown', showThumb); slider.removeEventListener('touchstart', showThumb); }; // 同时兼容鼠标点击和移动端触摸操作 slider.addEventListener('mousedown', showThumb); slider.addEventListener('touchstart', showThumb); }
内容的提问来源于stack exchange,提问作者Prashanti Ganesh
相关产品推荐
相关产品推荐

