Foros del Web » Programando para Internet » Jquery »

llamar a funcion JQuery para reiniciarla para scroll horizontal usando XAJAX.

Estas en el tema de llamar a funcion JQuery para reiniciarla para scroll horizontal usando XAJAX. en el foro de Jquery en Foros del Web. Hola, estoy usando una libreria JQUERY, para realizar un scroll horizontal: http://manos.malihu.gr/jquery-thumbnail-scroller Con esta estructura: Código HTML: <div id= "tS2" class= "jThumbnailScroller" > <div class= ...
  #1 (permalink)  
Antiguo 29/05/2012, 11:14
Avatar de Pentaxeros  
Fecha de Ingreso: mayo-2008
Mensajes: 173
Antigüedad: 15 años, 11 meses
Puntos: 3
llamar a funcion JQuery para reiniciarla para scroll horizontal usando XAJAX.

Hola, estoy usando una libreria JQUERY, para realizar un scroll horizontal:
http://manos.malihu.gr/jquery-thumbnail-scroller

Con esta estructura:
Código HTML:
<div id="tS2" class="jThumbnailScroller">
    <div class="jTscrollerContainer">
        <div class="jTscroller">
            <a href="#"><img src="thumbs/img1.jpg" /></a>
            ...
            <a href="#"><img src="thumbs/img5.jpg" /></a>
        </div>
    </div>
    <a href="#" class="jTscrollerPrevButton"></a>
    <a href="#" class="jTscrollerNextButton"></a>
</div> 

y después llamando la función JQUERY
Código HTML:
<script>
jQuery.noConflict();
(function($){
window.onload=function(){
    $("#tS2").thumbnailScroller({
        scrollerType:"hoverPrecise",
        scrollerOrientation:"horizontal",
        scrollSpeed:2,
        scrollEasing:"easeOutCirc",
        scrollEasingAmount:600,
        acceleration:4,
        scrollSpeed:800,
        noScrollCenterSpace:10,
        autoScrolling:0,
        autoScrollingSpeed:2000,
        autoScrollingEasing:"easeInOutQuad",
        autoScrollingDelay:500
    });
}
})(jQuery);
</script>
<script src="jquery.thumbnailScroller.js"></script> 

Funciona...
PERO MI PROBLEMA, es que el contenido del DIV class="jTscroller" lo genero dinamicamente con XAJAX.

La primera vez que se genera BIEN, queda todo OK, pero en el momento que cambio el contenido de forma dinamica, se queda mal, ya que la función de scroll no toma en cuenta si ahora contiene 5 10 o 50 elementos a mostrar.

Desde XAJAX cuando genero el nuevo contenido puedo ejecutar SCRIPT usando:
$respuesta->script('........');
return $respuesta;

La función es esta:
Código HTML:
/*
Author: malihu [http://manos.malihu.gr]
*/
(function($){  
 $.fn.thumbnailScroller=function(options){  
	var defaults={ //default options
		scrollerType:"hoverPrecise", //values: "hoverPrecise", "hoverAccelerate", "clickButtons"
		scrollerOrientation:"horizontal", //values: "horizontal", "vertical"
		scrollEasing:"easeOutCirc", //easing type
		scrollEasingAmount:800, //value: milliseconds
		acceleration:2, //value: integer
		scrollSpeed:600, //value: milliseconds
		noScrollCenterSpace:0, //value: pixels
		autoScrolling:0, //value: integer
		autoScrollingSpeed:8000, //value: milliseconds
		autoScrollingEasing:"easeInOutQuad", //easing type
		autoScrollingDelay:2500 //value: milliseconds
	};
	var options=$.extend(defaults,options);
    return this.each(function(){ 
		//cache vars
		var $this=$(this);
		var $scrollerContainer=$this.children(".jTscrollerContainer");
		var $scroller=$this.children(".jTscrollerContainer").children(".jTscroller");
		var $scrollerNextButton=$this.children(".jTscrollerNextButton");
		var $scrollerPrevButton=$this.children(".jTscrollerPrevButton");
		//set scroller width
		if(options.scrollerOrientation=="horizontal"){
			$scrollerContainer.css("width",999999); 
			var totalWidth=$scroller.outerWidth(true);
			$scrollerContainer.css("width",totalWidth);
		}else{
			var totalWidth=$scroller.outerWidth(true);
		}
		var totalHeight=$scroller.outerHeight(true); //scroller height
		//do the scrolling
		if(totalWidth>$this.width() || totalHeight>$this.height()){ //needs scrolling		
			var pos;
			var mouseCoords;
			var mouseCoordsY;
			if(options.scrollerType=="hoverAccelerate"){ //type hoverAccelerate
				var animTimer;
				var interval=8;
				$this.hover(function(){ //mouse over
					$this.mousemove(function(e){
						pos=findPos(this);
						mouseCoords=(e.pageX-pos[1]);
						mouseCoordsY=(e.pageY-pos[0]);
					});
					clearInterval(animTimer);
					animTimer = setInterval(Scrolling,interval);
				},function(){  //mouse out
					clearInterval(animTimer);
					$scroller.stop();
				});
				$scrollerPrevButton.add($scrollerNextButton).hide(); //hide buttons
			}else if(options.scrollerType=="clickButtons"){
				ClickScrolling();
			}else{ //type hoverPrecise
				pos=findPos(this);
				$this.mousemove(function(e){
					mouseCoords=(e.pageX-pos[1]);
					mouseCoordsY=(e.pageY-pos[0]);
					var mousePercentX=mouseCoords/$this.width(); if(mousePercentX>1){mousePercentX=1;}
					var mousePercentY=mouseCoordsY/$this.height(); if(mousePercentY>1){mousePercentY=1;}
					var destX=Math.round(-((totalWidth-$this.width())*(mousePercentX)));
					var destY=Math.round(-((totalHeight-$this.height())*(mousePercentY)));
					$scroller.stop(true,false).animate({left:destX,top:destY},options.scrollEasingAmount,options.scrollEasing); 
				});
				$scrollerPrevButton.add($scrollerNextButton).hide(); //hide buttons
			}
			//auto scrolling
			if(options.autoScrolling>0){
				AutoScrolling();
			}
		} else {
			//no scrolling needed
			$scrollerPrevButton.add($scrollerNextButton).hide(); //hide buttons
		}
		//"hoverAccelerate" scrolling fn
		var scrollerPos;
		var scrollerPosY;
		function Scrolling(){
			if((mouseCoords<$this.width()/2) && ($scroller.position().left>=0)){
				$scroller.stop(true,true).css("left",0); 
			}else if((mouseCoords>$this.width()/2) && ($scroller.position().left<=-(totalWidth-$this.width()))){
				$scroller.stop(true,true).css("left",-(totalWidth-$this.width())); 
			}else{
				if((mouseCoords<=($this.width()/2)-options.noScrollCenterSpace) || (mouseCoords>=($this.width()/2)+options.noScrollCenterSpace)){
					scrollerPos=Math.round(Math.cos((mouseCoords/$this.width())*Math.PI)*(interval+options.acceleration));
					$scroller.stop(true,true).animate({left:"+="+scrollerPos},interval,"linear"); 
				}else{
					$scroller.stop(true,true);
				}
			}
			if((mouseCoordsY<$this.height()/2) && ($scroller.position().top>=0)){
				$scroller.stop(true,true).css("top",0); 
			}else if((mouseCoordsY>$this.height()/2) && ($scroller.position().top<=-(totalHeight-$this.height()))){
				$scroller.stop(true,true).css("top",-(totalHeight-$this.height())); 
			}else{
				if((mouseCoordsY<=($this.height()/2)-options.noScrollCenterSpace) || (mouseCoordsY>=($this.height()/2)+options.noScrollCenterSpace)){
					scrollerPosY=Math.cos((mouseCoordsY/$this.height())*Math.PI)*(interval+options.acceleration);
					$scroller.stop(true,true).animate({top:"+="+scrollerPosY},interval,"linear"); 
				}else{
					$scroller.stop(true,true);
				}
			}
		}
		//auto scrolling fn
		var autoScrollingCount=0;
		function AutoScrolling(){
			$scroller.delay(options.autoScrollingDelay).animate({left:-(totalWidth-$this.width()),top:-(totalHeight-$this.height())},options.autoScrollingSpeed,options.autoScrollingEasing,function(){
				$scroller.animate({left:0,top:0},options.autoScrollingSpeed,options.autoScrollingEasing,function(){
					autoScrollingCount++;
					if(options.autoScrolling>1 && options.autoScrolling!=autoScrollingCount){
						AutoScrolling();
					}
				});
			});
		}
		//click scrolling fn
		function ClickScrolling(){
			$scrollerPrevButton.hide();
			$scrollerNextButton.show();
			$scrollerNextButton.click(function(e){ //next button
				e.preventDefault();
				var posX=$scroller.position().left;
				var diffX=totalWidth+(posX-$this.width());
				var posY=$scroller.position().top;
				var diffY=totalHeight+(posY-$this.height());
				$scrollerPrevButton.stop().show("fast");
				if(options.scrollerOrientation=="horizontal"){
					if(diffX>=$this.width()){
						$scroller.stop().animate({left:"-="+$this.width()},options.scrollSpeed,options.scrollEasing,function(){
							if(diffX==$this.width()){
								$scrollerNextButton.stop().hide("fast");
							}
						});
					} else {
						$scrollerNextButton.stop().hide("fast");
						$scroller.stop().animate({left:$this.width()-totalWidth},options.scrollSpeed,options.scrollEasing);
					}
				}else{
					if(diffY>=$this.height()){
						$scroller.stop().animate({top:"-="+$this.height()},options.scrollSpeed,options.scrollEasing,function(){
							if(diffY==$this.height()){
								$scrollerNextButton.stop().hide("fast");
							}
						});
					} else {
						$scrollerNextButton.stop().hide("fast");
						$scroller.stop().animate({top:$this.height()-totalHeight},options.scrollSpeed,options.scrollEasing);
					}
				}
			});
			$scrollerPrevButton.click(function(e){ //previous button
				e.preventDefault();
				var posX=$scroller.position().left;
				var diffX=totalWidth+(posX-$this.width());
				var posY=$scroller.position().top;
				var diffY=totalHeight+(posY-$this.height());
				$scrollerNextButton.stop().show("fast");
				if(options.scrollerOrientation=="horizontal"){
					if(posX+$this.width()<=0){
						$scroller.stop().animate({left:"+="+$this.width()},options.scrollSpeed,options.scrollEasing,function(){
							if(posX+$this.width()==0){
								$scrollerPrevButton.stop().hide("fast");
							}
						});
					} else {
						$scrollerPrevButton.stop().hide("fast");
						$scroller.stop().animate({left:0},options.scrollSpeed,options.scrollEasing);
					}
				}else{
					if(posY+$this.height()<=0){
						$scroller.stop().animate({top:"+="+$this.height()},options.scrollSpeed,options.scrollEasing,function(){
							if(posY+$this.height()==0){
								$scrollerPrevButton.stop().hide("fast");
							}
						});
					} else {
						$scrollerPrevButton.stop().hide("fast");
						$scroller.stop().animate({top:0},options.scrollSpeed,options.scrollEasing);
					}
				}
			});
		}
	});  
 };  
})(jQuery); 
//global js functions
//find element Position
function findPos(obj){
	var curleft=curtop=0;
	if (obj.offsetParent){
		curleft=obj.offsetLeft
		curtop=obj.offsetTop
		while(obj=obj.offsetParent){
			curleft+=obj.offsetLeft
			curtop+=obj.offsetTop
		}
	}
	return [curtop,curleft];
}
Alguna idea??? Yo de Java scrip no domino mucho, pero lo que necesito es poder "reiniciar" la función, y claro sin dominar pues me esta costando...

Gracias!!!
  #2 (permalink)  
Antiguo 29/05/2012, 12:06
Avatar de marlanga  
Fecha de Ingreso: enero-2011
Ubicación: Murcia
Mensajes: 1.024
Antigüedad: 13 años, 3 meses
Puntos: 206
Respuesta: llamar a funcion JQuery para reiniciarla para scroll horizontal usando XAJ

Código Javascript:
Ver original
  1. (function($){
  2. function crearScroller(etiqueta){
  3.     $(etiqueta).thumbnailScroller({
  4.         scrollerType:"hoverPrecise",
  5.         scrollerOrientation:"horizontal",
  6.         scrollSpeed:2,
  7.         scrollEasing:"easeOutCirc",
  8.         scrollEasingAmount:600,
  9.         acceleration:4,
  10.         scrollSpeed:800,
  11.         noScrollCenterSpace:10,
  12.         autoScrolling:0,
  13.         autoScrollingSpeed:2000,
  14.         autoScrollingEasing:"easeInOutQuad",
  15.         autoScrollingDelay:500
  16.     });
  17. }
  18. window.onload=function(){
  19.     crearScroller("#tS2");
  20. }
  21. })(jQuery);

y en el SUCCESS de la llamada AJAX haces otro
Código Javascript:
Ver original
  1. crearScroller("#tS2");

Si nada más entrar en la página (antes de que se ejecute el AJAX), no hay "scroll" de imágenes, puedes borrar la llamada a esa función el el window.onload
  #3 (permalink)  
Antiguo 29/05/2012, 12:38
Avatar de Pentaxeros  
Fecha de Ingreso: mayo-2008
Mensajes: 173
Antigüedad: 15 años, 11 meses
Puntos: 3
Respuesta: llamar a funcion JQuery para reiniciarla para scroll horizontal usando XAJ

Gracias, pruebo y te cuento...

Saludos
  #4 (permalink)  
Antiguo 29/05/2012, 13:01
Avatar de Pentaxeros  
Fecha de Ingreso: mayo-2008
Mensajes: 173
Antigüedad: 15 años, 11 meses
Puntos: 3
Respuesta: llamar a funcion JQuery para reiniciarla para scroll horizontal usando XAJ

Hola, lo he probado y NO reinicia el scroll.

Si en el inicio, el scroll era de 5 paginas, y genero un nuevo contenido de 2 paginas, el scroll sigue pensando que tengo 5 paginas...

Alguna idea mas???

Muchas gracias!!!
  #5 (permalink)  
Antiguo 29/05/2012, 13:14
Avatar de Pentaxeros  
Fecha de Ingreso: mayo-2008
Mensajes: 173
Antigüedad: 15 años, 11 meses
Puntos: 3
Respuesta: llamar a funcion JQuery para reiniciarla para scroll horizontal usando XAJ

Esa función actuar actúa, pero no donde debe.

Ya que si la variable
scrollerType:"hoverPrecise",
Le doy otros valores, de los disponibles, el tipo de Scroll cambia, pero el ancho y las páginas no varía.

Quizás sea necesario actuar sobre otras variables como ancho o posición, etc.

Saludos
  #6 (permalink)  
Antiguo 30/05/2012, 07:26
Avatar de Pentaxeros  
Fecha de Ingreso: mayo-2008
Mensajes: 173
Antigüedad: 15 años, 11 meses
Puntos: 3
Respuesta: llamar a funcion JQuery para reiniciarla para scroll horizontal usando XAJ

He conseguido algo, por ejemplo, usando la funcion que me decía marlanga, la he modificado a los parametros que uso yo, y he añadido totalWidth que de partida lo he puesto a 1500.

Código PHP:
(function($){
function 
crearScroller(etiqueta){
$(
etiqueta).thumbnailScroller({
scrollerType:"clickButtons",
scrollerOrientation:"horizontal",
scrollSpeed:2,
scrollEasing:"easeOutCirc",
scrollEasingAmount:600,
acceleration:4,
scrollSpeed:800,
noScrollCenterSpace:0,
autoScrolling:0,
autoScrollingSpeed:2000,
autoScrollingEasing:"easeInOutQuad",
autoScrollingDelay:500,
totalWidth:1500
});

Luego en el codigo JAVA ha modificado donde se creaba la variable totawidth, que era:
var totalWidth=$scroller.outerWidth(true);
por
var totalWidth=options.totalWidth;

Y ahora el scroll se crea acorde a ese ancho que yo indico.

Calcular desde PHP en la función XAJAX el ancho para mi es muy sencillo, ya que se al numero de imagenes y su ancho, por lo que tengo que ver como paso ahora eso a la función de Javascrip, para que en lugar de pasar 1500 me pase el valor que yo le diga.

Yo desde XAJAX llamaria asi:
crearScroller("#tS2",ancho);

pero como no domino el javascript, no se como pasar esa variable dentro de a función crearScroller

alguna ayuda???

Gracias!!!
  #7 (permalink)  
Antiguo 30/05/2012, 15:52
Avatar de Pentaxeros  
Fecha de Ingreso: mayo-2008
Mensajes: 173
Antigüedad: 15 años, 11 meses
Puntos: 3
Respuesta: llamar a funcion JQuery para reiniciarla para scroll horizontal usando XAJ

Alguna ayuda de como puedo pasar 2 o 3 variables a crearscroller y usarlas en la función??? de javascript no domino mucho la sintaxis y he hecho algunas pruebas y NO funciona.

Gracias!!!
  #8 (permalink)  
Antiguo 31/05/2012, 04:19
Avatar de Pentaxeros  
Fecha de Ingreso: mayo-2008
Mensajes: 173
Antigüedad: 15 años, 11 meses
Puntos: 3
Respuesta: llamar a funcion JQuery para reiniciarla para scroll horizontal usando XAJ

Hola, sigo comiendome la cabeza...

He conseguido pasar los datos a la funcion crearScroller. Lo hago asi:
Código HTML:
(function($){
function crearScroller(etiqueta,ancho){
 $(etiqueta).thumbnailScroller({
 scrollerType:"clickButtons",
 scrollerOrientation:"horizontal",
 scrollSpeed:2,
 scrollEasing:"easeOutCirc",
 scrollEasingAmount:600,
 acceleration:4,
 scrollSpeed:800,
 noScrollCenterSpace:0,
 autoScrolling:0,
 autoScrollingSpeed:2000,
 autoScrollingEasing:"easeInOutQuad",
 autoScrollingDelay:500,
 totalWidth:ancho,
 pos:0
 });
}
})
PERO, desde la funcion en XAJAX, ejecuto esto:
Código PHP:
$respuesta4->script('crearScroller("#tS2",'.($cont*150).');'); 
y no funciona, simplemente no actua, y no se porque.

Es como si no llamara a la funcion, y la funcion si funciona, ya que si la llamo desde
Código PHP:
window.onload=function(){
    crearScroller("#tS2",<?php echo 150*$cont;?>);
}
si funciona.

Alguien sabe decirme porque no me hace caso la función crearScroller cuando la llamo desde XAJAX con la orden script???
  #9 (permalink)  
Antiguo 31/05/2012, 04:32
Avatar de Pentaxeros  
Fecha de Ingreso: mayo-2008
Mensajes: 173
Antigüedad: 15 años, 11 meses
Puntos: 3
Respuesta: llamar a funcion JQuery para reiniciarla para scroll horizontal usando XAJ

He probado asi:
$respuesta4->script("jQuery('#tS2').crearScroller('#tS2','800' );");

y tampoco funciona!!!

Una ayuda please!
  #10 (permalink)  
Antiguo 14/06/2012, 01:50
Avatar de Pentaxeros  
Fecha de Ingreso: mayo-2008
Mensajes: 173
Antigüedad: 15 años, 11 meses
Puntos: 3
Respuesta: llamar a funcion JQuery para reiniciarla para scroll horizontal usando XAJ

Os cuento la solución para que sirva a alguien en el futuro...

Inserte esto en el html:
<div id="botonRESET" class="jTscrollerRESETButton"></div>

Luego en el archivo JS inserte esto:
Código PHP:
$scrollerRESETButton.click(function(e){ 
e.preventDefault();                        $scroller.stop().animate({left:0},options.scrollSpeed,options.scrollEasing);
$scrollerPrevButton.stop().hide("fast");
totalWidth=$scroller.outerWidth(true);
if (
totalWidth>850
{
    
$scrollerNextButton.stop().show("fast");
}
else
{
$scrollerNextButton.stop().hide("fast");
}

}); 
y despues desde XAJAX cada vez que genero el nuevo contenido hago esto:
Código PHP:
 $respuesta4->script("fireEvent(document.getElementById('botonRESET'),'click');"); 
que es esta funcion de javascrip.

Código PHP:
function fireEvent(obj,evt){
    var 
poscicionscroll=window.pageYOffset;
    var 
fireOnThis obj;
    if( 
document.createEvent ) {
      var 
evObj document.createEvent('MouseEvents');
      
evObj.initEventevtfalsefalse ); 
      
fireOnThis.dispatchEvent(evObj);
    } else if( 
document.createEventObject ) {
      
fireOnThis.fireEvent('on'+evt);
    }
        
    } 
y funciona!

Saludos!

Última edición por Pentaxeros; 14/06/2012 a las 02:01

Etiquetas: ajax, funcion, horizontal, js, scroll
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:41.