单击某个元素会更改它's的位置
Clicking an element changes it's position
我有一个函数,单击时会将点移动到屏幕中心。这样做的函数看起来是这样的:
// Animates the slider
animate: function (startTime, distance, duration, options, completedFn) {
// Animate
requestAnimationFrame(function () {
// Get our offset
var now = Date.now(),
timeDelta = Math.min(now - startTime, duration),
timePercent = timeDelta / duration,
offset = timePercent * distance;
// Get our position
var position = options.finalX + offset;
// Create the transform
options.transform = 'translateX(' + position + 'px)';
// Add to our element
options.element.style.transform = options.transform;
// If our time is less than our duration
if (timeDelta < duration) {
// Animate again
service.animate(startTime, distance, duration, options, completedFn);
// If our time has exceeded our duration
} else {
// Set our options for scrolling
options.finalX = position;
options.oldX = options.currentX;
options.currentX = options.currentX + (distance / options.width);
// Invoke our callback
completedFn();
}
});
},
// Moves the current active item to the center
moveToCenter: function (e, options, animate, completedFn) {
// Exit if we are scrolling
if (options.started)
return;
// Get the central position
var target = e.target,
rect = target.getBoundingClientRect(),
distance = options.center - rect.left - ((50 - 18) / 2), // Make these variables?
positive = rect.left > options.center;
console.log(rect);
// If we should animation
if (animate) {
// Get our start points
var distance = options.center - rect.left - ((50 - 18) / 2), // Make these variables?
now = Date.now(),
duration = Math.abs(distance) / 1;
// Animate our elements
service.animate(now, distance, duration, options, function () {
completedFn(target);
});
// Otherwise
} else {
// Update the current position
options.currentX = distance / options.speed;
// Move our elements
service.updateDragElement(options);
// Invoke our callback
completedFn(target);
}
},
和我一起玩的是:
distance = options.center - rect.left - ((50 - 18) / 2), // Make these variables?
50是可点击元素的宽度,18是实际点的宽度。这是一个代码笔,所以你可以看到问题。
当你加载笔时,如果你点击粉红色方块右边的点,它会滚动到粉红色方块中。问题是,如果你在点的中心点击它,它实际上会滚动到上一个的确切位置,但如果你在点的左侧或右侧点击它(但仍然可以点击),它会稍微偏离你点击它的一侧。
我有点困惑为什么会发生这种情况,因为当我调用moveToCenter时,点还没有移动,所以无论我在可点击元素中单击何处,它都应该具有相同的位置。
有人知道为什么会发生这种事吗?
对不起,我想我没有完全理解你的问题,但我能把一切都放在正确的中心。
首先禁用body
填充:
body {
padding: 0;
}
然后将您的距离更改为:
distance = options.center - rect.left - 18/2
我希望这会有所帮助。
编辑
好吧,我只是不明白。这里的问题是,气泡中心和外盒是不同的元素。用console.log(e.target)
进行测试。您可以使用getBoundingClientRect()
来查找必须移动的元素,在这种情况下,它的移动方式不同。如果你不需要外盒,那么你可以更换:
<a href="#" ng-click="moveToCenter($event, true)">
<div class="point" ng-class="{ 'active': item.active }" style="background-color: {{ item.color }}" ng-click="moveToCenter($event, true)"></div>
</a>
收件人:
<a href="#" ng-click="moveToCenter($event, true)" class="point" ng-class="{ 'active': item.active }" style="background-color: {{ item.color }}">
</a>
这样,只有气泡才能作为单击区域工作。
相关文章:
- 从鼠标点(及更多)查询位置元素
- 保持光标位置元素在顶部使用 svg
- 如何在每次页面重新加载时更改渐变背景,同时保持光标位置元素处于活动状态
- 使用JS暂时禁用在位置/元素处滚动
- 相对于另一个元素的位置元素不起作用
- 返回包含随机位置元素的数组
- 如何在 Jquery 中根据屏幕大小移动绝对位置元素
- 如何在html中设置固定位置元素的最大深度
- 无论如何,如果绝对位置元素位于屏幕下方,则使正文滚动条出现
- 绝对位置元素未隐藏在滚动框之外
- 为什么浮动位置 元素缩小时元素不移动
- 固定位置元素…动作
- 当元素处于相对位置元素时,查找元素偏离窗口的jQuery或JavaScript方法
- 绝对位置元素,这样它们的行为就像浮动一样
- jQuery鼠标在菜单和动画位置元素后面
- 捕获绝对位置元素上的单击事件
- 如何设置固定位置元素的高度以达到浏览器窗口的底部
- jQuery绑定位置:元素的绝对位置
- 如何将固定位置元素限制为浏览器窗口的高度
- 如何在不使用JS的情况下使用TOC滚动页面上的固定位置元素