p:carousel - How to pause animation on mouse over
Hi, after reading the Yahoo documentation, I've found the solution for the proposed problem.
How to pause p:carousel animation on mouse over:
All you need is to use the following script in the same page as p:carousel:
With this solution, when the mouse is over the carousel then the animation will stop. The animation starts again when the mouse leaves the carousel. Very easy!
PS.: If you have problems using the '$' variable, you can use 'jQuery' instead.
How to pause p:carousel animation on mouse over:
Code: Select all
<p:carousel id="carouselID" widgetVar="carouselNice"
value="#{lastMembers.list}" var="member" rows="4"
autoPlayInterval="4000" circular="true"
itemStyle="height: 200px; width: 150px">
Code: Select all
<script type="text/javascript">
$(document).ready( function() {
$('#carouselID').hover(
function() {carouselNice.stopAutoPlay()},
function() {carouselNice.startAutoPlay()}
);
});
</script>
PS.: If you have problems using the '$' variable, you can use 'jQuery' instead.
Provided code doesn't work for my carousel.
These two variants doesn't work:
But this works ok (for a fading effect):
Any help will be appreciated.
Code: Select all
<p:carousel id="carouselNews" widgetVar="wCarouselNews"...
Code: Select all
<script type="text/javascript">
jQuery(function() {
$('#carouselNews').hover(
function(){wCarouselNews.stopAutoPlay()},function(){wCarouselNews.startAutoPlay()}
);
});
</script>
Code: Select all
<script type="text/javascript">
jQuery(function() {
$('#carouselNews').hover(
function(){$(this).stopAutoPlay();$(this).startAutoPlay();}
);
});
</script>
Code: Select all
<script type="text/javascript">
jQuery(function() {
$('#carouselNews').hover(
function(){$(this).fadeOut(100);$(this).fadeIn(500);}
);
});
</script>
Any help will be appreciated.
PrimeFaces 3.4.2, Mojarra 2.0.6, Apache Tomcat/7.0.22
-
- Posts: 1
- Joined: 07 Dec 2012, 13:09
Previous code doesn't work for me. But I found a way to achieve it.
What I had to do is not to provide the autoPlayInterval in the p:carousel component, but put it inside my javascript in order to get the setIntervalId to use it later in the stop function.
XHTML CODE:
JS CODE:
What I had to do is not to provide the autoPlayInterval in the p:carousel component, but put it inside my javascript in order to get the setIntervalId to use it later in the stop function.
XHTML CODE:
Code: Select all
<p:carousel id="carouselID" widgetVar="carouselNice" pageLinks="5" rows="1" circular="true" itemStyle="border:none;height:365px;width:775px;">
<p:graphicImage name="slide1.png" library="img/about" />
<p:graphicImage name="slide2.png" library="img/about" />
<p:graphicImage name="slide3.png" library="img/about" />
<p:graphicImage name="slide4.png" library="img/about" />
<p:graphicImage name="slide5.png" library="img/about" />
</p:carousel>
Code: Select all
/*The the setIntervalId here is a global parameter*/
var idInterval = 0;
function startCarousel() {
/*The interval in milisecond coud be passed as a parameter, here it is fixed*/
carouselNice.cfg.autoPlayInterval = 6000;
idInterval = setInterval(function() {
carouselNice.next();
}, carouselNice.cfg.autoPlayInterval);
}
$(document).ready(function() {
startCarousel();
$('#carouselID').hover(function() {
clearInterval(idInterval);
}, function() {
startCarousel();
});
});
-
- Posts: 1
- Joined: 29 Nov 2017, 14:32
Try this. We have implemented this in our project and this works as a charm in PF 6.1.2
<p:carousel id="carouselID" widgetVar="carouselNice" pageLinks="5" rows="1" circular="true" itemStyle="border:none;height:365px;width:775px;">
<p:graphicImage name="slide1.png" library="img/about" />
<p:graphicImage name="slide2.png" library="img/about" />
<p:graphicImage name="slide3.png" library="img/about" />
<p:graphicImage name="slide4.png" library="img/about" />
<p:graphicImage name="slide5.png" library="img/about" />
</p:carousel>
JS Code
----------------------
$(window).load(function() {
var $this = PF('carouselNice');
if($this) {
var viewport = $this.jq.children('.ui-carousel-viewport');
viewport.on('hover.carousel mouseenter.carousel', function() {
$this.stopAutoplay();
}).on('mouseleave.carousel', function() {
$this.startAutoplay();
});
}
});
<p:carousel id="carouselID" widgetVar="carouselNice" pageLinks="5" rows="1" circular="true" itemStyle="border:none;height:365px;width:775px;">
<p:graphicImage name="slide1.png" library="img/about" />
<p:graphicImage name="slide2.png" library="img/about" />
<p:graphicImage name="slide3.png" library="img/about" />
<p:graphicImage name="slide4.png" library="img/about" />
<p:graphicImage name="slide5.png" library="img/about" />
</p:carousel>
JS Code
----------------------
$(window).load(function() {
var $this = PF('carouselNice');
if($this) {
var viewport = $this.jq.children('.ui-carousel-viewport');
viewport.on('hover.carousel mouseenter.carousel', function() {
$this.stopAutoplay();
}).on('mouseleave.carousel', function() {
$this.startAutoplay();
});
}
});
I was able to get this to work using the above example, but I'm trying to understand the mechanics of this better. For example, I'm familiar with 'component.hover...', but not 'component.on('hover...)'. I'm not sure how you knew to call out 'hover.carousel mouseenter.carousel' for instance. I looked in the js code, but didn't see that anywhere, and I'm trying to determine where I missed that.rahul.gupt@gmail.com wrote: ↑29 Nov 2017, 14:37Try this. We have implemented this in our project and this works as a charm in PF 6.1.2
<p:carousel id="carouselID" widgetVar="carouselNice" pageLinks="5" rows="1" circular="true" itemStyle="border:none;height:365px;width:775px;">
<p:graphicImage name="slide1.png" library="img/about" />
<p:graphicImage name="slide2.png" library="img/about" />
<p:graphicImage name="slide3.png" library="img/about" />
<p:graphicImage name="slide4.png" library="img/about" />
<p:graphicImage name="slide5.png" library="img/about" />
</p:carousel>
JS Code
----------------------
$(window).load(function() {
var $this = PF('carouselNice');
if($this) {
var viewport = $this.jq.children('.ui-carousel-viewport');
viewport.on('hover.carousel mouseenter.carousel', function() {
$this.stopAutoplay();
}).on('mouseleave.carousel', function() {
$this.startAutoplay();
});
}
});
Thank you!
When I tried this on 6.2, I get an error at run time in the browser console that the carouselNice object didn't support an indexOf() method.
I tried to use carouselId as the PF call and that didn't change. I realize that the calls to startAutoPlay and stopAutoPlay methods should be called on the widgetVar object.
I understand that 6.2 had a major jquery update so that may have broken this method.
Anyone running this on 6.2?
I tried to use carouselId as the PF call and that didn't change. I realize that the calls to startAutoPlay and stopAutoPlay methods should be called on the widgetVar object.
I understand that 6.2 had a major jquery update so that may have broken this method.
Anyone running this on 6.2?
-
- Information
-
Who is online
Users browsing this forum: No registered users and 57 guests