クリックしたときに異なるdivが関数内で同じタスクを実行するようにする方法があるのだろうかと思います。ここに私がこれまで持っているものがあります。しかし、このコードを実行すると、何も起こらないようですが、エラーは表示されません。どんな助けもありがとう。同じ関数を別のオブジェクトに適用する方法
function animation() {
var counter = counters[0];
if (counter < 80){
position = counter * (100/80);
$("._0").css({"background-position": position + "% 0%"});
counters[0]++;
} else {
counters[0] = 0;
}
}
などのような関数を呼び出す:私は以下のように、直接アニメーション関数にパラメータをプラグインしている場合
<html>
<head>
</head>
<body>
<div id="works">
<div class="dot" onclick="currentSlide(0)"></div>
<div class="dot" onclick="currentSlide(1)"></div>
<div class="dot" onclick="currentSlide(2)"></div>
<div class="dot" onclick="currentSlide(3)"></div>
</div>
<div class = "image">
<img class = "phone" src = "img/phone.png">
<div class = "ad _0"></div>
<div class = "ad _1"></div>
<div class = "ad _2"></div>
<div class = "ad _3"></div>
</div>
<script src="jquery-3.1.1.min.js"></script>
<script>
var slideIndex = 0,
frames =[80,154,64,49],
counters = [0,0,0,0];
showSlides(slideIndex);
function animation(ad, frames, counter) {
if (counter < frames){
position = counter * (100/frames);
$(ad).css({"background-position": position + "% 0%"});
counter ++;
} else {
counter = 0;
}
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var dots = document.getElementsByClassName("dot");
for (i = 0; i < dots.length; i++) {
$("._" + i).css({"visibility": "hidden"});
}
$("._" + slideIndex).css({"visibility": "visible"});
setInterval(animation("._" + slideIndex, frames[slideIndex], counters[slideIndex]),150);
}
</script>
</body>
</html>
setInterval(animation,150);
私は私が望む結果を得るが、手動で各パラメータをプラグインする必要はありません。\
*何も*起きていないようにみえ - 何をすべきかあなたは起こることを期待している? – RobG
基本的に、アニメーション化しているという錯覚を与えている各画像をパンすると仮定します。 – user2724072
あなたのコードの 'slideIndex'は常に' = 0'です。私は最後の関数 'showSlides'では' slideIndex'の代わりに 'n'から' slideIndex'に引数を変更するか、 'slideIndex'の代わりに' n'を使う必要があると思います。 –