[英]jquery onclick add margin-left
I'm trying something pretty simple in JS but I can't make it work... 我正在JS中尝试一些非常简单的方法,但无法使其正常工作...
I would like when clicking on a div to add a negative margin-left to another div, but I want it to happened everytime I click on the div, not only one time as it does now. 我想在单击div时向另一个div添加负的左边距,但是我希望每次单击div时都会发生这种情况,而不仅仅是现在这样。 Eveytime I click on my #next_nav, I would like the #nav to move from 10px negative.
Eveytime我单击我的#next_nav,我希望#nav从10px负值移动。 here it only works one time.
在这里它只能工作一次。 here is my js :
这是我的js:
$(function() {
$('#next_nav').click(function () {
$( "#nav" ).css('margin-left','-10px');
});
});
and my HTML : 和我的HTML:
<div id="next_nav"></div>
<div id="nav"></div>
here is my JSfiddle : http://jsfiddle.net/Beyzd/ 这是我的JSfiddle: http : //jsfiddle.net/Beyzd/
can anybody helps me with this ? 有人可以帮我吗? thanks a lot,
非常感谢,
You can try this 你可以试试这个
var pixels=0;
$(function() {
$('#next_nav').click(function () {
pixels=pixels-10;
$( "#nav" ).css('margin-left',pixels);
});
});
$(function() {
var count=0;
$('#next_nav').click(function () {
count-=10;
$( "#nav" ).css('margin-left',count);
});
});
And include jQuery 并包括jQuery
Fixed it for you: Updated version 已为您修复: 更新的版本
edit the .css('margin-left','-10px');
编辑
.css('margin-left','-10px');
to .css({'margin-left' : '-10px'});
到
.css({'margin-left' : '-10px'});
Try this: 尝试这个:
$(function() {
$('#next_nav').click(function () {
marginleft=parseInt($( "#nav" ).css('margin-left').replace('px',''));
$( "#nav" ).css('margin-left',marginleft-10+'px');
});
});
只需更改此$( "#nav" ).css('margin-left','-=10px');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.