In HTML, ogni evento del mouse ha accesso all'elemento sottostante. Puoi farlo facilmente con JavaScript, e c'è una bella funzionalità in HTML5 chiamata classList (grazie a Erik di Chromium) che ti permette di aggiungere e rimuovere facilmente classi da DOM.
Prima di tutto, è possibile ottenere questo con Google Chrome Content Scripts. L'algoritmo è piuttosto semplice, si mantiene un puntatore all'ultimo DOM visitato e si aggiunge/rimuove solo la classe quando si visita un altro elemento DIV.
All'interno di manifest.json Definiremo le iniezioni CSS e JS in ogni pagina visualizzata.
...
...
"content_scripts": [
{
"matches": ["http://*/*"],
"css": ["core.css"],
"js": ["core.js"],
"run_at": "document_end",
"all_frames": true
}
]
...
...
Vediamo ora nei nostri Core.js, ho incluso alcuni commenti per spiegare cosa sta succedendo:
// Unique ID for the className.
var MOUSE_VISITED_CLASSNAME = 'crx_mouse_visited';
// Previous dom, that we want to track, so we can remove the previous styling.
var prevDOM = null;
// Mouse listener for any move event on the current document.
document.addEventListener('mousemove', function (e) {
var srcElement = e.srcElement;
// Lets check if our underlying element is a DIV.
if (srcElement.nodeName == 'DIV') {
// For NPE checking, we check safely. We need to remove the class name
// Since we will be styling the new one after.
if (prevDOM != null) {
prevDOM.classList.remove(MOUSE_VISITED_CLASSNAME);
}
// Add a visited class name to the element. So we can style it.
srcElement.classList.add(MOUSE_VISITED_CLASSNAME);
// The current element is now the previous. So we can remove the class
// during the next iteration.
prevDOM = srcElement;
}
}, false);
Ora, consente di guardare al semplice core.css per gli stili:
.crx_mouse_visited {
background-color: #bcd5eb !important;
outline: 1px solid #5166bb !important;
}
Ecco fatto, si noterà che tutti i div avrà un "aleggiava" stato, simile a quello che HAP penne quando visiti l'ispettore del tuo browser durante l'ispezione degli elementi.
fonte
2010-12-15 01:51:50
Buona risposta, sostituirò solo 'bordo' con' contorno', aggiungendo bordi sposterei elementi in giro. Probabilmente è necessario anche '! Important' su css. – serg
Grazie mille lavorando bene Sii felice e goditi la vita. –
Grazie serg! Modificato il post per riflettere i tuoi commenti :) –