简体   繁体   English

向滑块添加10px“填充”

[英]Add 10px 'padding' to slider

I implemented a color slider based on this website , with the source file here . 我基于此网站实现了一个彩色滑块, 源文件在这里 I'm trying to modify it a bit, and got stuck. 我试图对其进行一些修改,并陷入困境。

What I'm trying to do is, make sort of a padding on the right and left side of the slider. 我想做的是在滑块的右侧和左侧进行某种填充。 Meaning, I don't want the slider to go all the way to either side, I want there to be 10px at the right and left side, so when you slide it, you won't be able to slide it all the way, you can slide it up to 10px on the right and left side. 意思是说,我不希望滑块一直滑到任一侧,我希望左右两侧都有10px ,所以当您滑动它时,您将无法一直滑动,您可以将其左右滑动最多10 10px

Hope this picture makes it clearer: 希望这张图可以使它更清晰:

在此处输入图片说明

I tried adding -10 to line 70, but it didn't give the desired effect. 我尝试在第70行中添加-10 ,但未达到预期效果。 Then I added it to line 56, and still didn't get the desired effect. 然后我将其添加到第56行,但仍未获得理想的效果。

How can I make the slider go up to 10px to the left, and not past 10px to the right? 如何使滑块向左上10px ,而不向右上10px

CodePen CodePen

 ; (function(window, undefined) { "use strict" // Some common use variables var ColorPicker = window.ColorPicker, Tools = ColorPicker || window.Tools, // provides functions like addEvent, ... getOrigin, etc. startPoint, currentTarget, currentTargetWidth = 0; /* ---------------------------------- */ /* ------- Render color patch ------- */ /* ---------------------------------- */ var testPatch = document.getElementById('testPatch'), renderTestPatch = function(color) { // used in renderCallback of 'new ColorPicker' var RGB = color.RND.rgb; testPatch.style.cssText = 'background-color: rgba(' + RGB.r + ',' + RGB.g + ',' + RGB.b + ',' + color.alpha + ');'; }; /* ---------------------------------- */ /* ---------- Color sliders --------- */ /* ---------------------------------- */ var sliders = document.getElementById('sliders'), sliderChildren = sliders.children, type, mode, max = { rgb: { r: [0, 255], g: [0, 255], b: [0, 255] }, hsl: { h: [0, 360], s: [0, 100], l: [0, 100] } }, sliderDown = function(e) { // mouseDown callback var target = e.target || e.srcElement, id, len; if (target !== this) { if (e.preventDefault) e.preventDefault(); currentTarget = target.id ? target : target.parentNode; id = currentTarget.id; // rgbR len = id.length - 1; type = id.substr(0, len); // rgb mode = id.charAt(len).toLowerCase(); // R -> r startPoint = Tools.getOrigin(currentTarget); currentTargetWidth = currentTarget.offsetWidth - 10; sliderMove(e); Tools.addEvent(window, 'mousemove', sliderMove); startRender(); } }, sliderMove = function(e) { // mouseMove callback var newColor = {}; // The idea here is (so in the HSV-color-picker) that you don't // render anything here but just send data to the colorPicker, no matter // if it's out of range. colorPicker will take care of that and render it // then in the renderColorSliders correctly (called in renderCallback). newColor[mode] = (e.clientX - startPoint.left) / currentTargetWidth * max[type][mode][1]; myColor.setColor(newColor, type); }, renderColorSliders = function(color) { // used in renderCallback of 'new ColorPicker' for (var n = sliderChildren.length; n--;) { var child = sliderChildren[n], len = child.id.length - 1, type = child.id.substr(0, len), mode = child.id.charAt(len).toLowerCase(); if (child.id) { child.children[0].style.width = ((color.RND[type][mode] - max[type][mode][0]) / (max[type][mode][1] - max[type][mode][0]) * 100) + '%'; } } }; Tools.addEvent(sliders, 'mousedown', sliderDown); // event delegation Tools.addEvent(window, 'mouseup', function() { Tools.removeEvent(window, 'mousemove', sliderMove); stopRender(); }); var doRender = function(color) { renderTestPatch(color); renderColorSliders(color); }, renderTimer, startRender = function(oneTime) { renderTimer = window.setInterval( function() { doRender(myColor.colors); // http://stackoverflow.com/questions/2940054/ }, 13); // 1000 / 60); // ~16.666 -> 60Hz or 60fps }, stopRender = function() { window.clearInterval(renderTimer); }, myColor = new Colors(); doRender(myColor.colors); })(window); 
 #testPatch { position: relative; left: 20px; width: 500px; height: 300px; } 
 <link href="https://rawgit.com/PitPik/colorPicker/master/index.css" rel="stylesheet" /> <div id="testPatch"></div> <div id="sliders" class="sliders"> <div id="rgbR"> <div></div> </div> <div id="rgbG"> <div></div> </div> <div id="rgbB"> <div></div> </div> <div id="hslH"> <div></div> </div> <div id="hslS"> <div></div> </div> <div id="hslL"> <div></div> </div> </div> <script src="https://rawgit.com/PitPik/colorPicker/master/colors.js"></script> <script src="https://rawgit.com/PitPik/colorPicker/master/colorPicker.js"></script> 

Not sure this breaks the required functionality, but it does provide the correct padding: 不确定这是否破坏了必需的功能,但确实提供了正确的填充:

.sliders>div {
  padding: 0 10px;
}

Since width is calculated in % you can't cut 10px precisly. 由于宽度是以%计算的,因此您无法精确切割10px。 A possible workaround is to move a starting point a little (eg 5) and reduce width from 100% (eg to 90%): 可能的解决方法是将起点稍微移动一点(例如5),并将宽度从100%(例如减少到90%):

child.children[0].style.width = ((color.RND[type][mode] - max[type][mode][0]) / (max[type][mode][1] - max[type][mode][0]) * 100) * 0.9 + 5 + '%';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM