I'm using Bootstrap's (3.3.5) Carousel to show a Slider on a page. Code is pretty simple and standard:
<div id="header-carousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="pull-right carousel-indicators">
<li data-target="#header-carousel" data-slide-to="0" class="active"></li>
<li data-target="#header-carousel" data-slide-to="1"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<article class="item active" style="background-image:url(foo.jpg);">
<div class="carousel-caption">
<h1>FOO</h1>
<h2>Caption</h2>
</div>
</article>
<article class="item" style="background-image:url(bar.jpg);">
<div class="carousel-caption">
<h1>BAR</h1>
<h2>Caption</h2>
</div>
</article>
</div>
</div>
I was missing the option to swipe on mobile devices, so I added jQuery mobile 1.4.5 to get this function like this:
$(document).ready(function() {
$("#header-carousel").swiperight(function() {
$(this).carousel('prev');
});
$("#header-carousel").swipeleft(function() {
$(this).carousel('next');
});
});
So far it works on mobile and tablets. But it also works on desktops, so one can't select the caption without triggering the swipe-methods. Is there a possibility to deactivate this behaviour?
I would ask you to disable it only for those devices without Touch support. So for that, you can try this:
function isTouchSupported () {
try {
document.createEvent("TouchEvent");
touch = true;
} catch (e) {
touch = false;
}
return touch;
}
The same program can be written in another way too:
function isTouchSupported () {
return 'ontouchstart' in document.documentElement;
}
And after initializing, you can unbind the events:
if (!isTouchSupported())
$("#header-carousel").off("swiperight").off("swipeleft");
edit: added () to isTouchSupported-call
Self Proof Check:
On my desktop Google Chrome, using F12 Developer Tools:
On my mobile Google Chrome, using jsBin's console tab.
Related
I'm new to jQuery and I have some issues to stop the scrolling effect. For context, I use this code for hide and show some section on button click.
This is my code:
jQuery(function($) {
$('.rv_button1').click(function(e) {
e.preventDefault();
$("#reveal1").slideToggle();
$('.rv_button1').toggleClass('opened closed');
});
});
I use wordpress with divi theme, the html is not really clear but this part is where my jQuery function is made for (sorry for my English though)
<div class="et_pb_button_module_wrapper et_pb_button_0_wrapper et_pb_button_alignment_center et_pb_module ">
<a class="et_pb_button et_pb_button_0 rv_button et_pb_bg_layout_light opened" href="http://valerieb30.sg-host.com/notre-equipe/#reveal">Québec</a>
</div>
<div id="reveal" class="et_pb_row et_pb_row_1 et_pb_equal_columns et_pb_gutters2" style="display: block;">
<div class="et_pb_column et_pb_column_4_4 et_pb_column_1 et_pb_css_mix_blend_mode_passthrough et-last-child">
<div class="et_pb_module et_pb_text et_pb_text_0 et_pb_text_align_left et_pb_bg_layout_light">
<div class="et_pb_text_inner"><h2>Cuisinistes</h2></div>
</div>
<div class="et_pb_module et_pb_de_mach_archive_loop et_pb_de_mach_archive_loop_0 main-loop loadmore-align- grid-layout-grid main-archive-loop
I have previously tried materialboxed for card images and it worked.
But when I use materialboxed for carousel images the result is different, not as good as the card image.
The fixed navigation bar and any of the elements below also look different from the card image, for which all the surrounding elements are invisible.
I have tried to edit css from materialize but still can't.
<div class="carousel">
<a class="carousel-item" href="#one!"><img src="https://lorempixel.com/250/250/nature/1" class="materialboxed"></a>
<a class="carousel-item" href="#two!"><img src="https://lorempixel.com/250/250/nature/2" class="materialboxed"></a>
<a class="carousel-item" href="#three!"><img src="https://lorempixel.com/250/250/nature/3" class="materialboxed"></a>
<a class="carousel-item" href="#four!"><img src="https://lorempixel.com/250/250/nature/4" class="materialboxed"></a>
<a class="carousel-item" href="#five!"><img src="https://lorempixel.com/250/250/nature/5" class="materialboxed"></a>
</div>
the result image materialboxed in carousel
You wrote:
The navigation bar and any of the elements below also look different from the card image, for which all the surrounding elements are invisible.
Does this mean that when you are using the class materialboxed with the img tags in the carousel that the rest of the page does not become invisible/blacked-out when you click on the image?
Perhaps you have more code in your document that you did not share?
I was able to achieve the materialboxed effect in your carousel with this code:
<!DOCTYPE html>
<html>
<head>
<!-- IMPORT MATERIALIZE CSS-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
</head>
<body>
<nav>
<div class="nav-wrapper">
Logo
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li>Sass</li>
<li>Components</li>
<li>JavaScript</li>
</ul>
</div>
</nav>
<div class="carousel">
<a class="carousel-item" href="#one!"><img src="https://lorempixel.com/250/250/nature/1" class="materialboxed"></a>
<a class="carousel-item" href="#two!"><img src="https://lorempixel.com/250/250/nature/2" class="materialboxed"></a>
<a class="carousel-item" href="#three!"><img src="https://lorempixel.com/250/250/nature/3" class="materialboxed"></a>
<a class="carousel-item" href="#four!"><img src="https://lorempixel.com/250/250/nature/4" class="materialboxed"></a>
<a class="carousel-item" href="#five!"><img src="https://lorempixel.com/250/250/nature/5" class="materialboxed"></a>
</div>
<!-- IMPORT MATERIALIZE JS -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<!-- INIT CAROUSEL -->
<script>
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.carousel');
var instances = M.Carousel.init(elems);
});
</script>
<!-- INIT MATERIALBOXED -->
<script>
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.materialboxed');
var instances = M.Materialbox.init(elems);
});
</script>
</body>
</html>
See screen grab for result.
EDIT 2020-10-23:
Several assumptions for the following:
materialize.js is now local to the html file instead of imported from CDN. Also, the <nav> tag is now wrapped in a div with class="navbar-fixed" and id="fixed-nav":
<div id="fixed-nav" class="navbar-fixed">
<nav>
{...}
</nav>
</div>
In comments, OP clarified that he doesn't like the behavior when using materialboxed + carousel + navbar-fixed.
One solution would be to remove the class navbar-fixed from <nav>'s wrapper div when opening a carousel image and adding it back when closing.
At line 3590 and 3722 of materialize.js are the functions to open and close, respectively, an image with class materialboxed. Inside these functions, you can add the following to remove/add navbar-fixed from your wrapper with id fixed-nav:
Inside the open() function:
// remove navbar-fixed
let fixedNav = document.getElementById('fixed-nav')
fixedNav.classList.remove('navbar-fixed')
Inside the close() function:
// add navbar-fixed
let fixedNav = document.getElementById('fixed-nav')
fixedNav.classList.add('navbar-fixed')
I have built a bootstrap video carousel. It is working just fine but, the only problem I have is the carousel keeps sliding to the next slide after 5 seconds. How do I make it stop autosliding and only slide when the user clicks on the left or right arrows? I have pasted the code below.
<div class="container">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel" data-pause=hover>
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<div class="embed-responsive embed-responsive-16by9">
<video class="embed-responsive-item" autoplay muted id="homevid">
<source src="C:\Development Software\Sample Website\videos\Michael Vick 88 yard touchdown pass to DeSean Jackson.mp4" />
</video>
</div>
<div class="carousel-caption">
</div>
</div>
<div class="item">
<div class="embed-responsive embed-responsive-16by9">
<video class="embed-responsive-item" autoplay muted id="homevid">
<source src="C:\Development Software\Sample Website\videos\Vick to Jeremy Maclin for 50 Yard TD.mp4" />
</video>
</div>
</div>
<div class="item">
<div class="embed-responsive embed-responsive-16by9">
<video class="embed-responsive-item" autoplay muted id="homevid">
<source src="C:\Development Software\Sample Website\videos\Michael Vick Scramble Plays vs Rams 2011.mp4" />
</video>
</div>
</div>
</div>
By adding data-interval="false"
<div id="carousel-example-generic" class="carousel slide" data-interval="false" data-ride="carousel" data-pause="hover" >
From the documentation
Option - Interval - ..If false, carousel will not automatically cycle.
2021 Update
In Bootstrap 5 it is data-bs-interval="false"
<div id="carousel-example-generic" class="carousel slide" data-bs-interval="false" data-ride="carousel" data-pause="hover">
Documentation
There are two ways.
1.
Update 2022: Bootstrap 5.x
In your HTML code, you need to use data-bs-interval="false"
<div id="your-carousel-id" class="carousel slide" data-bs-interval="false">
</div>
Bootstrap 4.x
In your HTML code, you need to use data-interval="false"
<div id="your-carousel-id" class="carousel slide" data-interval="false">
</div>
2.
If you want to do with the help of Jquery then you need to add.
// document ready
$(document).ready(function(){
// Event for pushed the video
$('#your-carousel-id').carousel({
pause: true,
interval: false
});
});
Thanks :)
To stop the autoplay is by just removing the attribute data-ride=”carousel” from the htmlcode
<div id="carousel-example" class="carousel slide" data-ride="carousel">....</div>
There is another way to stop autoplay just add below code in your js file
$(window).load(function() {
$('.carousel').carousel('pause');
});
Paste this code on your custom Javascript file.
$('.carousel').carousel({
interval: false
});
In react-bootstrap in order to stop cycling you have to use
<Carousel interval={null}>
...
</Carousel>
There is an update on bootstrap
https://getbootstrap.com/docs/5.1/components/carousel/
data-bs-interval="false"
for bootstrap v5.2:
Just remove "data-bs-ride:carousel" part from the div tag having "carousel slide" in order to stop the auto-slide
By adding data-interval="false" and removing data-ride="carousel" to the carousel id or class
I spent a lot of time on why data-bs-ride=false does not work for this and i understand it now.
In version 5.0 and 5.1, notice in documentation under options subsection it says
Autoplays the carousel after the user manually cycles the first item. If set to 'carousel', autoplays the carousel on load.
This just means that data-bs-ride=false, data-bs-ride=true or data-ride="carousel" it will still auto slide so we have to use
data-interval="false" and remove data-ride="carousel"
But in version 5.2 the documentation says that
If set to true, autoplays the carousel after the user manually cycles the first item. If set to "carousel", autoplays the carousel on load.
So, we can stop the autoplay by just using data-bs-ride=false or just removing data-bs-ride tag bc it is by default false.
I want to embed a youtube video in a bootstrap one page scroll web page, exactly in a carousel video slider.
I have a problem with both Chrome and Firefox (no problem using Safari).
Here is my code:
<section class="videos">
<div class="container">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel" data-interval="false">
<!-- Indicators -->
<ol class="carousel-indicators" style="top:125%">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
</ol>
<div class="carousel-inner" style="top:100px;">
<div class="item active">
<div style="text-align:center;">
<iframe width="853" height="480" src="http://www.youtube-nocookie.com/embed/XXXX?rel=0" frameborder="0" allowfullscreen></iframe>
<div class="carousel-caption">
</div>
</div>
</div>
<div class="item">
<div style="text-align:center;">
<iframe width="853" height="480" src="http://www.youtube-nocookie.com/embed/XXXX?rel=0" frameborder="0" allowfullscreen></iframe>
<div class="carousel-caption">
</div>
</div>
</div>
</div>
I get two error:
In my console I get:
Blocked a frame with origin "https://www.youtube-nocookie.com" from accessing a frame with origin "http://www.domain.com". The frame requesting access has a protocol of "https", the frame being accessed has a protocol of "http". Protocols must match.
Uncaught Error: Error calling method on NPObject.
The videos, appears as black boxes.
What should I do? Thanks
I'd recommend you using an object for the videos you want to embed in your site instead of the iframe. It has a better web browser independent behaviour and works as well with other video providers that enable sharing (dailymotion, vimeo)
<embed width="756" height="461" style="display: block;" src="http://www.youtube.com/v/nlcIKh6sBtc?autohide=1&version=3?autohide=1&version=3&autoplay=1" type="application/x-shockwave-flash" bgcolor="#000000" scale="scale" allowfullscreen="true" salign="tl" allowscriptaccess="never" wmode="opaque">
The above code snippet is an example of how to share a youtube video on a web page with various parameters. Hope this helped.
I'm using iScroll for a horizontal, fixed position menu. It works great, however I'm unable to vertically scroll the rest of the page in a mobile browser. It works fine in a pc/mac resized browser window, just not on mobile. I've tried Safari on iPhone and Chrome and Dolphin on Android.
Any ideas how I can get the page to scroll too?
I've played with the vScroll, hScroll, etc options but haven't been able to solve the issue:
<nav id="mainNav">
<ul class="pagesIcons bottom">
<li style="background-color:#000000" class="active selected current youarehere">
<a href="#" class="inner">
<div class="buttonIcon">
<img src="/content/icons/002.png">
</div>
<div class="buttonText">
Homepage
</div>
</a>
</li>
<li style="background-color:#383838" class="">
<a href="#" class="inner">
<div class="buttonIcon">
<img src="/content/icons/002.png">
</div>
<div class="buttonText">
Gallery
</div>
</a>
</li>
<li style="background-color:#5c5c5c" class="">
<a href="#" class="inner">
<div class="buttonIcon">
<img src="/content/icons/004.png">
</div>
<div class="buttonText">
Events
</div>
</a>
</li>
........
</ul>
</nav>
I'm initialising it with:
var myScroll;
function loaded() {
myScroll = new iScroll('mainNav', {
bounce: false
});
}
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', loaded, false);
Solved. I just had to remove:
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
Pretty obvious. This was preventing the default event on 'touchmove'. Durrrrrrr
You can fix this issue by applying this jquery, In place of ClassName you have to add the name of wrapper class in which you want scrolling, If you still face problem let me know I will try to help more.
$('.ClassName').bind('touchmove', function(e){
e.stopPropagation();
});