Estoy tratando de abrir un fancybox desde una función que tengo; en resumen, mi código HTML se ve así;
<a href="#modalMine" onclick="myfunction(this); return false;"> click </a>y una parte de mi función se ve así;
function myfunction(me) { $(me).fancybox({ 'autoScale': true, 'transitionIn': 'elastic', 'transitionOut': 'elastic', 'speedIn': 500, 'speedOut': 300, 'autoDimensions': true, 'centerOnScroll': true, }); }Lo anterior funciona en IE pero no en Firefox o Chrome. ¿Alguna idea de cómo puedo solucionar esto? Sé que uno de los motivos es activar otro enlace, pero espero que sea posible otra solución.
Si desea simplemente abrir un fancybox cuando se llama a una función de javascript. Tal vez en su flujo de código y no como resultado de un clic. Así es como lo haces:
function openFancybox() { $.fancybox({ 'autoScale': true, 'transitionIn': 'elastic', 'transitionOut': 'elastic', 'speedIn': 500, 'speedOut': 300, 'autoDimensions': true, 'centerOnScroll': true, 'href' : '#contentdiv' }); }Esto crea el cuadro usando "contentdiv" y lo abre.
Ya que está utilizando jQuery, deje de vincular controladores de eventos en su HTML y comience a escribir JavaScript discreto.
$(document).ready(function () { function myfunction(me) { $(me).fancybox({ 'autoScale': true, 'transitionIn': 'elastic', 'transitionOut': 'elastic', 'speedIn': 500, 'speedOut': 300, 'autoDimensions': true, 'centerOnScroll': true // remove the trailing comma!! }).click(); // fire the click event after initializing fancybox on this element // this should open the fancybox } // use .one() so that the handler is executed at most once per element $('a[href=#modalMine]').one('click', function () { myfunction(this); return false; }); });Sin embargo, particularmente no veo una razón para configurar el fancybox al hacer clic. Podrías hacer esto en su lugar:
$(document).ready(function () { function myfunction() { // note the use of "this" rather than a function argument $(this).fancybox({ 'autoScale': true, 'transitionIn': 'elastic', 'transitionOut': 'elastic', 'speedIn': 500, 'speedOut': 300, 'autoDimensions': true, 'centerOnScroll': true }); } $('a[href=#modalMine]').each(myfunction); });Lo que necesitas es:
$.fancybox.open({ .... });Consulte la sección "Métodos API" en la parte inferior de aquí: