简体   繁体   English

用jQuery动画元素位置

[英]Animate element position with jQuery

I have a slideshow im trying to edit. 我正在尝试编辑幻灯片。

JS JS

value = 100;
value = value++;
setTransformValue(eventsWrapper, 'translateX', value+'px'); 

HTML 的HTML

<div class="events" style="width: 4000px; transform: translateX(0px);">

I have an element named events that has a translate X value of 0 by default, I want to write a function that either adds or removes 100px from its current translate X value. 我有一个名为events的元素,默认情况下其translate X值为0,我想编写一个函数,该函数可以从当前translate X值中添加或删除100px。

the code I have written above adds 100, or takes away 100 from 0, not from the elements current value. 我在上面编写的代码将100加或从0(而不是元素当前值)中减去100。

Does this make sense? 这有意义吗?

Please check out this JSBin: http://jsbin.com/qayedelubi/edit?html,console,output 请查看以下JSBin: http ://jsbin.com/qayedelubi/edit?html,控制台,输出

The main idea is to extract the current value, add 100 to it and then set it to the element. 主要思想是提取当前值,将其添加100,然后将其设置为元素。

First, reference the element you want to manipulate: 首先,引用您要操作的元素:

var events = document.querySelector('div.events');

Then, extract it's current translateX value, using replace to strip everything but the actual numeric value. 然后,提取它的当前translationX值,并使用replace去除除实际数值之外的所有内容。

var translateX = parseInt(events.style.transform.replace('translateX(', '').replace('px)', ''), 10);
events.style.transform = 'translateX(' + (translateX + 100) + 'px)';

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

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