简体   繁体   English

在jquery滑块的两侧设置margin-left缓冲区

[英]Set margin-left buffer on both sides of jquery slider

I'm using a jquery slider and my slider handle is pretty big. 我正在使用jquery滑块,而我的滑块手柄很大。 In order for it to be the way I want it has to have margin-left: -2px when it's at 0 (all the way to the left) and have margin-left: -32px when it's at 100 (all the way to the right). 为了使其成为我想要的方式,它必须有margin-left: -2px当它为0时(一直向左))和有margin-left: -32px当它为100时(一直到)对)。

I want to ease into the correct margin rather than setting it upruptly. 我想放宽到正确的边距,而不要过分设置。 It can be done using the slide event but I'm having a little trouble with the calculation: 可以使用slide事件来完成,但是我在计算时遇到了一些麻烦:

$(...).slider({
     slide: function(event, ui){
         var newMarginLeft = ...calculation... (ui.value is 0-100)
         $(this).children(".ui-handle").css("marginLeft", newMarginLeft);
     }
});

You can use this function, which will generate numbers from 2 to 32 evenly: 您可以使用此函数,它将平均生成2到32之间的数字:

function generateNum(num)
{
    return 2 + Math.floor(num * .30);
}

Than you will just add a negative onto it. 比起,您只需在上面添加一个底片即可。 Use like this: 像这样使用:

$(...).slider({
     slide: function(event, ui){
         var newMarginLeft = "-" + generateNum(ui.value);
         $(this).children(".ui-handle").css("marginLeft", newMarginLeft + "px");
     }
})

jsFiddle here: http://jsfiddle.net/RWuR4/1/ jsFiddle在这里: http : //jsfiddle.net/RWuR4/1/

You can also try using Math.round to round up: Here is a fiddle that uses Math.round, which makes 99 also return 32 (so it might be a bit more of what you want instead of Math.floor): 您还可以尝试使用Math.round进行四舍五入:这是一个使用Math.round的小提琴,它使99也返回32(因此可能比您想要的要多一些而不是Math.floor):

http://jsfiddle.net/RWuR4/2/ http://jsfiddle.net/RWuR4/2/

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

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