Sto cercando di capire le migliori pratiche in materia di prestazioni durante la creazione di più DIV a una velocità folle. Ad esempio, su ogni evento .mousemove ...Creazione div cruscotto, jquery/javascript, performance/best practice
$('head').append("<style>.draw {width: 20px; height: 20px; position:fixed;</style>");
$(document).mousemove(function(mouseMOVE) {
//current mouse position
var mouseXcurrent = mouseMOVE.pageX;
var mouseYcurrent = mouseMOVE.pageY;
//function to create div
function mouseTRAIL(mouseX, mouseY, COLOR) {
$('body').append("<div class='draw' style='top:" + mouseY + "px; left:" + mouseX + "px; background: " + COLOR + ";'></div>");
}
// function call to create <div> at current mouse positiion
mouseTRAIL(mouseXcurrent, mouseYcurrent, '#00F');
// Remove <div>
setTimeout(function() {
$('.draw:first-child').remove();
}, 250);
});
Quindi, questo funziona tutto bello e dandy, ma è inefficiente mega (specialmente quando provo riempiendo lo spazio tra ogni posizione di spostamento del mouse). Ecco un esempio ...
$('head').append("<style>.draw {width: 20px; height: 20px; position:fixed;</style>");
$(document).mousemove(function(mouseMOVE) {
//current mouse position
var mouseXcurrent = mouseMOVE.pageX;
var mouseYcurrent = mouseMOVE.pageY;
// function to create div
function mouseTRAIL(mouseX, mouseY, COLOR) {
$('body').append("<div class='draw' style='top:" + mouseY + "px; left:" + mouseX + "px; background: " + COLOR + ";'></div>");
}
// function call to create <div> at current mouse positiion
mouseTRAIL(mouseXcurrent, mouseYcurrent, '#00F');
// variabls to calculate position between current and last mouse position
var num = ($('.draw').length) - 3;
var mouseXold = parseInt($('.draw:eq(' + num + ')').css('left'), 10);
var mouseYold = parseInt($('.draw:eq(' + num + ')').css('top'), 10);
var mouseXfill = (mouseXcurrent + mouseXold)/2;
var mouseYfill = (mouseYcurrent + mouseYold)/2;
// if first and last mouse postion exist, function call to create a div between them
if ($('.draw').length > 2) {
mouseTRAIL(mouseXfill, mouseYfill, '#F80');
}
// Remove <div>
setTimeout(function() {
$('.draw:first-child').remove();
$('.draw:nth-child(2)').remove();
}, 250);
});
Io davvero non posso figura fuori come migliorare le cose. Credimi, ho provato a fare ricerche ma non ha funzionato molto ... Quello che sto cercando sono alcuni suggerimenti, esempi o collegamenti a migliori pratiche ...
Si noti che mi sto insegnando a codificare . Sono uno studente di Graphic Design e questo è il modo in cui sto trascorrendo la mia estate fuori dalla classe ... Realizzando piccoli progetti per insegnarmi JavasSript, cose divertenti :)
Ive ha creato alcuni jsfiddles per mostrare a cosa sto lavorando. ..
Mouse Trail, More Elements - molto molto lento
Mouse Trail, Less Elements - molto lento
Mouse Trail, Bare Bones - lento
+1 per un progetto molto interessante.dovresti renderlo un plugin jQuery! Funziona anche senza problemi, Chrome 19 su Win7 – Joseph
Cos'è molto molto lento? Funziona benissimo sul mio macbook – maxdec
Very Very Slow funziona per me. E lo amo. – kapa