jQuery的函数应该重复每次点击触发器(但它没有) [英] Jquery Function should repeat everytime a click the trigger (but it doesnt)
问题描述
好吧,我有这个美丽的小网站,它有一个div overflows:hidden
不适合包装的内容。我想要 margin-top:$ value-120px
每次我点击一个跨度 class'd .maisfilme
。这是我得到的
$(document).ready(function(){
$(。maisfilme ).on(click,function(){
var $ mtop = $(this).css('margin-top');
var $ vai =(parseInt($ ); $ b $(#filme)。css({
'margin-top': - + $ vai
});
});
});
它实际上完成了这项工作。它获得边缘顶部,增加它的整数值+ 120px,在它之前放一个 - 所以,滑动div。
真正的问题是它只发生一次。如果再次单击 .maisfilme
,则不会有任何反应。
有人可以知道我该怎么做,因此每次时都会执行该函数。 maisfilme
被点击了?
第二次你做到了 - ( - 110 + 110),这不会像你想要的那样增加幅度。你需要改变你的标志的逻辑,如果你希望它每次更负面。
另外你从获取css (this)
,但将其设置为 $(#filme)
,所以每次都不会增加,除非碰巧同一个对象。每次点击它们需要保持一致。
为了让它每次都变得更负面,您可以这样做:
$ b $($ .b$ $ $code $ $(document).ready(function(){
$(。maisfilme)。 {
var $ mtop = $(this).css('margin-top');
var $ vai =(parseInt($ mtop) - 110)+px;
$(this).css({
'margin-top':$ vai
});
});
});
或略有不同的版本,如果margin-top的初始值为不是零(更像你的第一次迭代):
$(document).ready(function(){
($。maisfilme)。on(click,function(){
var $ mtop = $(this).css('margin-top');
var $ vai =( - (Math.abs(parseInt($ mtop))+ 110))+px;
$(this).css({$ b $'margin-top':$ vai
});
});
});
Well, I have this beautifull little site wich has a div that overflows:hidden
the content that doesnt fit the wrapper. I want to margin-top: $value-120px
everytime i click a span class'd .maisfilme
. This is what i got
$(document).ready(function (){
$(".maisfilme").on("click", function() {
var $mtop = $(this).css('margin-top');
var $vai = (parseInt($mtop)+ 110 + "px");
$("#filme").css ({
'margin-top' : "-"+$vai
});
});
});
It actually does the job. It gets margin-top, increases its int value +120px, put a - before it and so, slides the div up.
The real problem is that it only happens ONCE. If I click .maisfilme
once more, nothing happens.
Could someone know what should I do so the function acts everytime .maisfilme
is clicked?
The second time you do it it's going to be -(-110 + 110) which isn't increasing the magnitude like you want. You need to change the logic of your sign if you want it to go more negative each time.
In addition you are fetching the css from $(this)
, but setting it on $("#filme")
so it won't go up each time unless those happen to be the same object. Those need to be the same for it to accumulate each time you click.
To make it go more negative each time, you could do this:
$(document).ready(function (){
$(".maisfilme").on("click", function() {
var $mtop = $(this).css('margin-top');
var $vai = (parseInt($mtop) - 110) + "px";
$(this).css ({
'margin-top' : $vai
});
});
});
or a slightly different version which gives a slightly different result if the initial value of the margin-top is not zero (more like what your first iteration does):
$(document).ready(function (){
$(".maisfilme").on("click", function() {
var $mtop = $(this).css('margin-top');
var $vai = (-(Math.abs(parseInt($mtop)) + 110)) + "px";
$(this).css ({
'margin-top' : $vai
});
});
});
这篇关于jQuery的函数应该重复每次点击触发器(但它没有)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!