さて、あなたは、あなたが問題を持っており、それはすでに良いことだところ認識:)
もう少しコンパクトなコードを作成し、ローカルスコープの外に、できるだけ多くのものを得るためにと始めました次の実装をチェックできます。
私は可能な限り多くのコメントを追加しようとしたのは、ちょっとしたデモです。
IDの代わりにクラス名を使用したいと思った後にもう少し編集しました:)その結果、私はちょっとだけ自由を与えるdocument.querySelectorAll
を使用しています。
有効なセレクタでstartPop関数を呼び出すことができます。あなたはクラス
startPop('.className');
自体も追加たとえば行きたい場合は、別の関数、NLトリガが、それはどのようにあなたを示しています
startPop('#elementId');
のか:あなたは純粋にIDにポップアップ表示したい場合は、使用することができます機能を開始/停止することができます。
また、setTimeout
メソッドの代わりにsetInterval
メソッドを使用することを選択しました。どちらの関数も一定のミリ秒後に関数をコールバックしますが、setIntervalは一度だけ呼び出す必要があります。
さらに、stopPopは関数と同じ自由度を持つように、document.querySelectorAll
を使用するようになりました。
startPop関数に2つのオプションのパラメータ、つまりtotal
とcallback
を追加しました。
合計は、要素を「点滅」させたい時間を示し、コールバックはポップが終了したときに通知を受け取る方法を提供します(ポップを開始した可能性のある要素を更新するなど)
私はあなたがjavascriptの
'use strict';
function getElements(className) {
// if it is a string, assume it's a selector like #id or .className
// if not, assume it's an element
return typeof className === "string" ? document.querySelectorAll(className) : [className];
}
function startPop(className, total, callback) {
// get the element once, and asign a value
var elements = getElements(className),
current = 0;
var interval = setInterval(function() {
var opacity = ++current % 2;
// (increase current and set style to the left over part after dividing by 2)
elements.forEach(function(elem) { elem.style.opacity = opacity });
// check if the current value is larger than the total or 10 as a fallback
if (current > (total || 10)) {
// stops the current interval
stopPop(interval, className);
// notifies that the popping is finished (if you add a callback function)
callback && callback();
}
}, 50);
// return the interval so it can be saved and removed at a later time
return interval;
}
function stopPop(interval, className) {
// clear the interval
clearInterval(interval);
// set the opacity to 1 just to be sure ;)
getElements(className).forEach(function(elem) {
elem.style.opacity = 1;
});
}
function trigger(eventSource, className, maximum) {
// get the source of the click event (the clicked button)
var source = eventSource.target;
// in case the attribute is there
if (!source.getAttribute('current-interval')) {
// start it & save the current interval
source.setAttribute('current-interval', startPop(className, maximum, function() {
// completed popping (set the text correct and remove the interval)
source.removeAttribute('current-interval');
source.innerText = 'Start ' + source.innerText.split(' ')[1];
}));
// change the text of the button
source.innerText = 'Stop ' + source.innerText.split(' ')[1];
} else {
// stop it
stopPop(source.getAttribute('current-interval'), className);
// remove the current interval
source.removeAttribute('current-interval');
// reset the text of the button
source.innerText = 'Start ' + source.innerText.split(' ')[1];
}
}
<div class="nav1">
test navigation
</div>
<div class="nav2">
Second nav
</div>
<div class="nav1">
second test navigation
</div>
<div class="nav2">
Second second nav
</div>
<a id="navigation-element-1"
onmouseover="this.interval = startPop(this)"
onmouseout="stopPop(this.interval, this)">Hover me to blink</a>
<button type="button" onclick="trigger(event, '.nav1', 100)">
Start nav1
</button>
<button type="button" onclick="trigger(event, '.nav2', 100)">
Start nav2
</button>
でも、あなたは既に問題を認識しました。これは、あなたが 'nav1'パラメータをハードコーディングしたという事実です。ここでは、関数にパラメータを追加する方法を知っていると仮定しています。これらのパラメータを関数内で再利用できます。あなたが考えなければならない別のことは、文字列のアプローチでsetTimeoutは、非常に日付の付いたaproachに慣れています。匿名関数を定義し、その無名関数内からpop関数を呼び出してください。もう一つの小さな点は、これは作業コードですので、[codereview](http://codereview.stackexchange.com)はあなたの質問のためのより良い場所になるかもしれません – Icepickle
私の関数にパラメータを追加すると動作するようになりました(試しました)。私はパラメータのgetElementByIdすることができないようです。 "+ 'p1 +'" 'を試してみてください。 setTimeoutはうまくいくかもしれませんが、onmouseoverの機能IDが必要でしょうか? – MrMeDo