Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Pessoal tenho o seguinte código javascript abaixo.
Ele pega a posição do mouse no eixo Y.
Esse código funciona bem no chrome, mas no firefox ele apresenta o seguinte erro:
No caso ele apresenta "a is not defined", mas a variável a é a variável "event", que eu envio por parâmetro.
Alguel sabe por o firefox não reconhece?
Já procurei na internet, mas não encontrei resposta.
function f15Mouse() {}
f15Mouse.prototype.getPosition = function(event) {
var dot, eventDoc, doc, body, pageX, pageY;
event = event || window.event; // IE-ism
// If pageX/Y aren't available and clientX/Y are,
// calculate pageX/Y - logic taken from jQuery.
// (This is to support old IE)
if (event.pageX == null && event.clientX != null) {
eventDoc = (event.target && event.target.ownerDocument) || document;
doc = eventDoc.documentElement;
body = eventDoc.body;
event.pageX = event.clientX +
(doc && doc.scrollLeft || body && body.scrollLeft || 0) -
(doc && doc.clientLeft || body && body.clientLeft || 0);
event.pageY = event.clientY +
(doc && doc.scrollTop || body && body.scrollTop || 0) -
(doc && doc.clientTop || body && body.clientTop || 0 );
}
return event.pageY;
}Eu tenho uma classe que gerencia uma modal. A modal pode ser aberta quando usuário for sair da página, ou depois de x segundos.
Segue o código:
function f15Modal2() {
this.cssId = "";
this.closeByShade = false;
this.animacao = 1;
this.lockkk = false;
this.lockkkTime = 0;
this.lockkkDuration = 0;
this.horaAtual = "";
this.scrollObj;
this.mouseObj;
// Animação
// 1 - Depois de X segundo;
// 2 - Quando chegar até o final da página
// 3 - Quando o scroll tiver movido até X pixel. Exemplo (300px);
// 4 - Quando o usuário for sair da página
this.timeToShow = 10;
this.openXTimes = 0; // quantidades de vezes que pode abrir
this.urlsAutorizadas = ["/contato", "/index.php"];
}
f15Modal2.prototype.setHoraAtual = function (valor){
this.horaAtual = valor;
}
f15Modal2.prototype.setScroll = function (obj){
this.scrollObj = obj;
}
f15Modal2.prototype.setMouse = function (obj){
this.mouseObj = obj;
}
f15Modal2.prototype.setLockkkDuration = function (valor){
this.lockkkDuration = valor;
}
// Se o modal estiver com lock true, significa que ele não poderá ser exibido
f15Modal2.prototype.setLockkk = function (valor){
this.lockkk = valor;
}
// Tempo que a modal ficará fechada
f15Modal2.prototype.setLockkkTime = function (valor){
this.lockkkTime = valor;
}
f15Modal2.prototype.getAnimacao = function() {
return this.animacao;
}
f15Modal2.prototype.setAnimacao = function (valor){
this.animacao = valor;
}
f15Modal2.prototype.getCssId = function() {
return this.cssId;
}
f15Modal2.prototype.setObjetoModal = function(id) {
this.objetoModal = document.getElementById(id);
}
f15Modal2.prototype.setId = function (id){
this.cssId = id;
}
// Se for true, quando o usuário clicar na area fora da modal, ele da close
f15Modal2.prototype.setShade = function (valor){
this.closeByShade = valor;
}
f15Modal2.prototype.setTimeToShow = function (valor){
this.timeToShow = valor;
}
f15Modal2.prototype.inicialize = function() {
var closeFunction = this.close;
var cssId = this.cssId;
var showModalFunction = this.showModal;
// Quando clica fora da modal
if(this.closeByShade == true) {
// Ele fecha a modal quando clica fora
document.querySelector("#" + this.cssId + " .shade").addEventListener('click', function() {
closeFunction(cssId);
}, false);
}
// Quando clica no X para fechar a modal
document.querySelector("#" + this.cssId + " .f15close").addEventListener('click', function() {
closeFunction(cssId);
}, false);
// Faz o controle para libera o do lock do modal
if(this.lockkk == true) {
var segundosPassados = Math.floor(this.horaAtual - this.lockkkTime);
var tempoParaFicarOff = this.lockkkDuration * 60;
console.log("Segundos passados " + segundosPassados);
console.log("Tempo para ficar off " + tempoParaFicarOff);
if(tempoParaFicarOff < segundosPassados) {
this.lockkk = false;
}
}
// Controla a animaçãoo 1
if (this.animacao == 1 && this.lockkk == false) {
setTimeout(function() {
showModalFunction(cssId, registraCookie);
}, this.timeToShow * 1000);
}
// Quando o usuário for sair da página
if (this.animacao == 2 && this.lockkk == false) {
var scrollF15 = this.scrollObj;
var mouseF15 = this.mouseObj;
setTimeout( function() {
document.addEventListener("mousemove", controlaTempo);
function controlaTempo() {
var posicaoMouse = mouseF15.getPosition(event) - scrollF15.getScroll()[1];
if(posicaoMouse < 30) {
showModalFunction(cssId, registraCookie);
document.removeEventListener("mousemove", controlaTempo);
}
}
} , 5000);
}
}
// Registra no cookie a quantidade de vezes que essa modal foi aberta :)))))
// Teste
function registraCookie(cssIDVALOR) {
f15CookieValuesObject["samuel"] = "Samuel Gomes Huarachi";
f15CookieValuesObject["samuel"] = "Samuel Gomes Huarachi - 2";
console.log("MEU NOMEEE: " + f15CookieValuesObject.samuel);
console.log(JSON.stringify(f15CookieValuesObject));
console.log(f15CookieValues);
console.log("Meu IDDDD" + cssIDVALOR);
console.log(f15CookieValuesObject.samuel);
// F15CookieInstancia.set(seosa_code, JSON.stringify(f15CookieValuesObject), 2); // Cria o cookie valido por 2 dias
console.log("Registrando cookie");
console.log("SEOSA CODE " + seosa_code);
}
f15Modal2.prototype.close = function(id) {
document.getElementById(id).style.display = "none";
}
f15Modal2.prototype.showModal = function(id, callback) {
document.getElementById(id).style.display = "block";
callback.call(this, id);
}
Eu chamo a função com problema da linha na parte:
var posicaoMouse = mouseF15.getPosition(event) - scrollF15.getScroll()[1];
mouseF15.getPosition(event);Eu carrego a modal da seguinte forma:
var f15geral = new f15Geral();
var f15Modal2 = new f15Modal2();
var f15CookieValuesObject = JSON.parse( f15CookieValues );
var f15Scroll = new f15Scroll();
var f15Mouse = new f15Mouse();
// Configura o Modal 2
f15Modal2.setId("f15Modal2");
f15Modal2.setShade(true);
f15Modal2.setTimeToShow(10);
f15Modal2.setAnimacao(2);
f15Modal2.setScroll(f15Scroll);
f15Modal2.setMouse(f15Mouse);
if (typeof f15CookieValuesObject.abriuModalLoginFacebookLock != "undefined") {
if (f15CookieValuesObject.abriuModalLoginFacebookLock == "true") {
f15Modal2.setLockkk(true);
}
}
if (typeof f15CookieValuesObject.abriuModalLoginFacebookLockTime != "undefined") {
f15Modal2.setLockkkTime(f15CookieValuesObject.abriuModalLoginFacebookLockTime);
}
if (typeof f15CookieValuesObject.abriuModalLoginFacebookLockTimeUnlock != "undefined") {
f15Modal2.setLockkkDuration(f15CookieValuesObject.abriuModalLoginFacebookLockTimeUnlock);
}
f15Modal2.setHoraAtual(f15geral.getSegundos());
// estamos pronto = document ready
function estamos_prontos() {
f15Modal2.inicialize();
}o erro está aqui:
var posicaoMouse = mouseF15.getPosition(event) - scrollF15.getScroll()[1];
você não tá pegando esse event de lugar nenhum.
você deveria no mínimo ter recebido o event daqui:
function controlaTempo(event) {Muito bom garoto!
Isso mesmo!!!
Vou ficar mais atento nesses pontos.
Obrigado!!!
como você chama essa função?