假设我的总宽度是585px。我希望将空间划分为相等的部分,并为每个部分分配一个位置内的索引值。如果我有6个区段,我可以这样做:(由总宽度/区段数分配)
//Set up elements with variables
this.sliderContent = config.sliderContent;
this.sectionsWrap = config.sectionsWrap;
//Selects <a>
this.sectionsLinks = this.sectionsWrap.children().children();
//Create drag handle
this.sectionsWrap.parent().append($(document.createElement("div")).addClass("handle-containment")
.append($(document.createElement("a")).addClass("handle ui-corner-all").text("DRAG")));
//Select handle
this.sliderHandle = $(".handle");
var left = ui.position.left,
position = [];
var position = ((left >= 0 && left <= 80) ? [0, 1] :
((left >= 81 && left <= 198) ? [117, 2] :
((left >= 199 && left <= 315) ? [234, 3] :
((left >= 316 && left <= 430) ? [351, 4] :
((left >= 431 && left <= 548) ? [468, 5] :
((left >= 549) ? [585, 6] : [] ) ) ) ) ) );
if (position.length) {
$(".handle").animate({
left : position[0]
}, 400);
Slider.contentTransitions(position);
}但是如果我有x个部分呢。这些部分只是像这样的元素
<li><a></a></li>
<li><a></a></li>
<li><a></a></li>或
<div><a></a></div>
<div><a></a></div>
<div><a></a></div>
<div><a></a></div>我如何划分585px的总和,并根据.handle元素当前的左值对索引进行位置分类?我可以通过使用ui.position.left知道拖动手柄的位置,我想要的是能够为每个元素设置索引,并且能够根据手柄在索引元素中的位置来设置手柄的动画。因为每个元素都是索引的,所以我稍后调用一个转换方法,并传入当前要显示的index #。我上面展示的代码可以工作,但效率不是很高。我还需要考虑手柄的宽度,以适应部分的宽度。http://jsfiddle.net/yfqhV/1/

发布于 2013-03-06 19:58:30
好的,问题中的范围数字之间的差异有一点不一致,这使得我的虚构单词de jour =)很难准确地算法如下:
如果您可以调整,我有一个解决方案-它不是特别聪明的JavaScript,所以很可能有更好的方法来做到这一点(所以JS的人,请随时教育我!)但它是有效的。
首先,我们需要一个函数来查找数组范围的索引,一个给定值在其中(这将替换嵌套的if-else缩写),然后我们有一个函数来设置位置数组,最后我们可以执行范围搜索并返回相应的值数组。
这个解决方案应该动态处理不同数量的部分,只要下面这一行:
var len = $("#sectionContainer").children().length;相应地进行调整。可能需要调整的其他值只有:
var totalWidth = 585;
var xPos = 81;虽然您可以设置它们,但如果您有可以从中提取值的元素,则会使其更具动态解决方案。
/**
* function to find the index of an array element where a given value falls
* between the range of values defined by array[index] and array[index+1]
*/
function findInRangeArray(arr, val){
for (var n = 0; n < arr.length-1; n++){
if ((val >= arr[n]) && (val < (arr[n+1]))) {
break;
}
}
return n;
}
/**
* function to set up arrays containing positional values
*/
function initPositionArrays() {
posArray = [];
leftPosArray = [];
var totalWidth = 585;
var xPos = 81;
var len = $("#sectionContainer").children().length;
var unit = totalWidth/(len - 1);
for (var i=1; i<=len; i++) {
pos = unit*(i-1);
posArray.push([Math.round(pos), i]);
xMin = (i >= 2 ? (i==2 ? xPos : leftPosArray[i-2] + posArray[1][0]) : 0);
leftPosArray.push(Math.round(xMin));
}
}
var left = ui.position.left;
initPositionArrays();
// find which index of "leftPosArray" range that "left" falls within
foundPos = findInRangeArray(leftPosArray, left);
var position = posArray[foundPos];
if (position.length) {
$(".handle").animate({
left : position[0]
}, 400);
Slider.contentTransitions(position);
}我已经设置了一个jsFiddle来演示。
享受吧!
编辑
我看过@JonnySooter自己的答案,虽然它正确地计算了位置,但它不会处理可变数量的部分。
为了让它能够处理任意数量的节,handleContainment div (它是动态创建的)需要动态设置宽度(通过内联样式)。
这是通过将分段数乘以每个分段的宽度(实际上与滑块的宽度相同)来计算的。
这都是在创建句柄之后完成的,这样就可以从" handle“css类中提取宽度,这意味着在css级别应用句柄时,对句柄宽度的更改将级联到例程中。
请参阅此jsFiddle,其中可以更改分段数,并且滑块可以正常工作。
发布于 2013-03-06 01:36:48
var numSections = // ...;
var totalWidth = // ...;
var sectionWidth = totalWidth / numSections;
var index = Math.floor($(".handle").position().left / sectionWidth);
var leftPosition = index * sectionWidth;
var rightPosition = leftPosition + sectionWidth - 1;发布于 2013-03-07 23:32:58
更新
我试着自己找到一个解决方案,这就是我想出来的:
function( event, ui ) {
var left = ui.position.left, //Get the current position of the handle
self = Slider, //Set to the Slider object cus func is a callback
position = 1;
sections_count = self.sectionsLinks.length, //Count the sections
section_position = Math.floor(self.sectionsWrap.width() / sections_count); //Set width of each section according to total width and section count
left = Math.round(left / section_position); //Set the index
position = (left * section_position); //Set the left ammount
if(position < section_position){ //If handle is dropped in the first section
position = 0.1; //Set the distance to animate
left = 0; //Set index to first section
}
if (position.length) {
$(this).animate({
left : position //Animate according to distance
}, 200);
left = left += 1; //Add one to the index so that I can use the nth() child selector later.
self.contentTransitions(left);
}
}https://stackoverflow.com/questions/15229600
复制相似问题