我想增加100%的點擊左邊緣。jquery點擊添加css值的百分比
所以說我現在的利潤率爲100% 然後在點擊= 200% 在第二次點擊= 300% 等等...
我該怎麼辦呢?我搜遍了,找不到解決辦法。添加100個像素很容易,但我需要百分比值。
謝謝!
我想增加100%的點擊左邊緣。jquery點擊添加css值的百分比
所以說我現在的利潤率爲100% 然後在點擊= 200% 在第二次點擊= 300% 等等...
我該怎麼辦呢?我搜遍了,找不到解決辦法。添加100個像素很容易,但我需要百分比值。
謝謝!
對於瀏覽器的一致性,我會建議你使用這種代碼段:
EDITED如下評論
$('.slide-back').data('marginLeft',10).on('click',function(){
var $inner = $('.inner'), curMarg = $(this).data('marginLeft') + 10;
$inner.css({marginLeft:curMarg+'%'});
$(this).data('marginLeft',curMarg);
});
thx @Ramaraj Karuppusamy for fixed typo –
謝謝!這工作,但是我有一個與'內部'類的按鈕和類'回退'的按鈕。所以當按鈕被點擊時,我不想將邊距添加到按鈕,而是我想將邊距添加到div'內部'。 ($''。slide-back')。data('marginLeft',10).on('click',function(){var 0 this = $(this),curMarg = $ this.data( 'marginLeft')+ 10; \t $ this.css({marginLeft:curMarg + '%'})。數據( 'marginLeft',curMarg); \t \t});' – beliy333
多少元件與類.inner?如果只有一個,修復很容易找到...順便說一句,閱讀你的問題,對不起,我無法推測它! –
我不明白你爲什麼做這樣的事情。 (保證金百分比)。
但在這裏,它是: Check Out LIVE DEMO
jQuery的
$('.mydiv').click(function()
{
var currentMargin = parseInt($(this).css('margin-left'));
$(this).css('margin-left',currentMargin+100+'%');
});
HTML
<div class='mydiv'></div>
CSS
.mydiv
{
background-color:red;
width:100px;
height:100px;
position:absolute;
margin-left:100%;
}
我真的不明白的問題太多,但,如果你嘗試獲得保證金一倍,在PX,%,或什麼的,你可以這樣做的(基於烤小提琴):
$('b').click(function(){
var curMargStr = $(this).css('marginLeft');
var curMarg = curMargStr.replace('px','') * 2;
$(this).css('marginLeft', curMarg);
});
您無法獲得百分比的值,因爲瀏覽器必須在px中進行轉換才能呈現該值。 請注意,值可能會因您使用的瀏覽器而異。
@roasted的代碼可以工作,但是我有一個帶有'inner'類的div和帶有'slide-back'類的按鈕。所以,當按鈕被點擊時,我想要將邊距添加到div'inner'而不是按鈕'slide-back'。 ($''。slide-back')。data('marginLeft',10).on('click',function(){var 0 this = $(this),curMarg = $ this.data('marginLeft')+ 10; \t $ this.css({marginLeft:curMarg +'%'})。data('marginLeft',curMarg); \t}); ?這是爲我正在創建的wordpress插件。它在你的文章中找到imgs,並將它們放入響應滑塊中。一旦它正常工作,我將分享這些代碼。 – beliy333
你嘗試過什麼嗎?請閱讀[FAQ](http://stackoverflow.com/faq) –