Showing posts with label CSS. Show all posts
Showing posts with label CSS. Show all posts

Saturday, 21 January 2017

Bootstrap Carousel - Different intervals for differnet slides

Problem:

Bootstrap Carousel is an easy and convenient option for implementing a slider on your website. But is there a way to advance the slides with different time intervals. For example, slide 1 will advance after 10 seconds, the second slide after 5 seconds and slide 3 to advance after 3 seconds. I came across a scirpting suggestion using the the carousel method with the 'pause' parameter. i.e. Pausing the slide advancing for amount of time computed as the slide timer as reduced by a the interval. But for some reasons this solution wasn't working for me. But I could have it working with a different approach, which is explained below, so that this will be useful for others as well.

Solution:

Instead of using the pause attribute, I tried using a script to advance the slides. i.e. advance the slides manually by calling carousel method the 'next' attribute. This can be accomplished in combination with the setTimeOut function. For e-g, something like this should help in having this working.

    var t;
    var start = $('#myCarousel').find('.active').attr('data-interval');

    t = setTimeout(function () { $('#myCarousel').carousel('next') }, start);

The block above will ensure that the first slide advances after the interval set for the first slide. Subsequent slide advances can be hanlded using the slid event as below.

    $('#myCarousel').on('slid.bs.carousel', function () {
     
        clearTimeout(t);
        var duration = $('#myCarousel').find('.active').attr('data-interval');

        t = setTimeout("$('#myCarousel').carousel('next');", duration);
    })

Hope this helps. Please to note that to have this working, use the data-interval attribute and set the desired interval in milli-seconds for each slide. e-g.

    <div class="item" data-interval="4000">


I have found this working fine with a single Carousel in the same page and have not tested this with multiple Carousels in the same page.

Saturday, 20 April 2013

Zoom and Pan Background Image

Question:

We are working on a project that needs quite a bit of complex image handling in a web browser. For simple image viewing as a gallery there are quite many Javascript libraries out there. Our unique requirement is to load template images with standard body part markings and then allow medical practitioners to mark their observations against the specific body parts. With the introduction of canvas element, HTML5 has made this a little bit easier. As the use cases need us to have multiple layers of images and markers with ability to zoom and pan, we were exploring options to have the ability to zoom and pan a background image so that we would be able to overlay additional layers containing markers and labels on top of it. Can you suggest how to get this done?

Question by: Sangeetha Sundram.

Answer:

Kinetic Javascript library is a good candidate for accomplishing lots of image handling abilities that you have described here. It supports adding a wide ranging shapes and even SVG paths into layers and then on to a stage container. In terms of HTML, these are canvas elements.

Since your specific question is about the ability to zoom and pan the background image, I suggest the following approach using jQuery library:

Use the Mouse Move in conjunction with the Mouse Down and Mouse Up events to do the image panning. This can be accomplished by manipulating the background-position-x and background-position-y css properties based on the current mouse co-ordinates as the mouse move happens. Use the Mouse Down event to initiate the pan action and then Mouse Up event to stop the pan. It would be appropriate to use the Mouse Up on the document as against the specific element as moving outside the element's boundary should mean to end the panning.

For Zoom in and Out, the background-size is the css property that you should be manipulating and you may derive a suitable algorithm to set a higher (for Zoom In) or lower (for Zoom Out) percentage to this attribute and you will have the background image zoomed in or out accordingly. Depending on your needs, you may want to use the background-position property as well to have the image zoomed in at the point of click and thus keeping the point of click in the visible frame.

Check out the Javascript code in action for simple image pan here on jsfiddle. I have extended it a bit to make the pan to happen on mouse click and move (drag) and also for Zoom In and you can check this out here at jsfiddle. You may further improve this code to suit your needs and do share it here so that it will be useful for all the developers.

Hope this is of help to you.