Bootstrap Carousel - Linked Image With Caption - html

This is my first carousel project and I have a question on the proper/preferred way of adding hyperlinks to the images. Here is my item format (using w3school's example):
<div class="item"><a href="videos/Reports.mp4" target="_blank">
<img src="graphics/reportsCarousel.png" /></a>
<a href="videos/Reports.mp4" target="_blank"><div class="carousel-caption">
<p>Reports</p>
</div></a>
</div>
Notice I have both the image and the caption linked, because the caption div caused a dead spot so that only the top and bottom of the image was active, i.e., clickable. I tried putting the caption in the image, but didn't like the results. I also tried moving the caption above and below the image, but the caption didn't display. I really like the way this looks, but wonder if there's a better way to set up the link?
Extra Credit: Also, I wonder if there is a way to have two or three images display side-by-side using the current div setup? When I put the second image into the item div the images display vertically instead of horizontally, even using an inline ul. I can do it using a more complicated method, but if I can easily tweak this one it would save me a lot of time.

I had some time today and put this together. It seems to work.
However, I am not sure it addresses your issues since, lacking a [mcve], I cannot reproduce your issues.
I added the JS to log the click event to console - you may or may not need it in your final version (again I am unsure of what you need since the code you provided is not complete/verifiable)
Please note
that the runnable snippet below is inside a frame and the x-frame-options is set to SAMEORIGIN so the link will not actually work here (you will need to copy/paste into your own page to see the links work)
$(function() {
$('.item').on('click', function() {
console.log('clicked! going to: ' + $(this).find('a').attr('href'));
window.location = $(this).find('a').attr('href');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
<li data-target="#myCarousel" data-slide-to="3"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<a href="http://google.com" target="_blank">
<img src="http://lorempixel.com/1024/480/" />
<div class="carousel-caption">
<h3>caption 1</h3>
</div>
</a>
</div>
<div class="item">
<a href="http://google.com" target="_blank">
<img src="http://lorempixel.com/1024/480/" />
<div class="carousel-caption">
<h3>caption 2</h3>
</div>
</a>
</div>
<div class="item">
<a href="http://google.com" target="_blank">
<img src="http://lorempixel.com/1024/480/" />
<div class="carousel-caption">
<h3>caption 3</h3>
</div>
</a>
</div>
<div class="item">
<a href="http://google.com" target="_blank">
<img src="http://lorempixel.com/1024/480/" />
<div class="carousel-caption">
<h3>caption 4</h3>
</div>
</a>
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>

Doesnt't work. With that code you find the first href ever.
Try this one:
$(function() {
$( ".item" ).each(function(index) {
$(this).on("click", function() {
console.log('clicked! going to: ' + $(this).find('a').attr('href'));
});
});
});

Related

HTML Bootstrap Carousel (carousel-control)

About the issue that currently facing is that for Bootstrap Carousel (from w3school) , there is a problem for carousel-control.
Problem that i am facing is that when i have enter hyperlink for the image , the carousel-control are larger then the image itself (screenshot below).
Screenshot: https://prnt.sc/r4wh2f
Codes for my bootstrap carousel
<div class="row">
<div class="col-12 col-md-12 banner-section">
<div class="container">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner">
<div class="item active">
<a href="facebook.com"><picture>
<source media="(max-width: 900px)" srcset="image/banner01small.jpg">
<img src="image/banner01" alt="banner" style="z-index:1" class="img-fluid">
</picture></a>
</div>
<div class="item">
<a href="instagram.com"><picture>
<source media="(max-width: 900px)" srcset="image/banner02small.jpg">
<img src="image/banner02" alt="banner" style="z-index:1" class="img-fluid">
</picture></a>
</div>
</div>
<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="glyphicon glyphicon-chevron-right"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</div>
</div>
Hope this help you. Demo
Just Added CSS below mentioned code
a.carousel-control {
width: 30px;
height: 30px;
top: 47%;
}
This is because the controls are also using the anchor tag and is conflicting with the same.you can add additional css to control anchor tags of the carousal and it will work fine.
All the best :)

Bootstrap Carousel Buttons Are Having Problems

I am working on a website, and Twitter Bootstrap's carousel controls don't seem to be working. Clicking on indicators allows me to navigate, and the .carousel() function works, so the website scrolls through the images without any problems, but when I try to navigate using the buttons, I can move one image to either way, but after that clicking on the button just sends me back to the previous animation, before sending me to the slide I was already on. Here's my code
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div id="mainCarousel" class="carousel slide" data-ride="carousel" data-interval="6000">
<ol class="carousel-indicators">
<li data-target="mainCarousel" data-slide-to="0" id="cib1" class="active"></li>
<li data-target="#mainCarousel" data-slide-to="1" id="cib2"></li>
<li data-target="#mainCarousel" data-slide-to="2" id="cib3"></li>
<li data-target="#mainCarousel" data-slide-to="3" id="cib4"></li>
</ol>
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="/res/img/carousel/image1.png" />
</div>
<div class="item">
<img src="/res/img/carousel/image2.png" />
</div>
<div class="item">
<img src="/res/img/carousel/image3.png" />
</div>
<div class="item">
<img src="/res/img/carousel/image4.png" />
</div>
</div>
<a class="left carousel-control" href="#mainCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#mainCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<script>
$("#mainCarousel").carousel();
</script>
Sorry if I explained it badly. I can't give anything visual. It seems to only be a problem with the website I'm making, because the code snippet works perfectly on here. In short, the left navigation button, for instance, will take me to the left once, but every time I click it after that, it moves to the right with no animation, before sliding back to the left, leaving me on the same slide I started on.
EDIT:
I am using Angular JS with routing on the same website, so that might affect how the carousel works.
See if this works, you have a few small errors in your code. You added / after the the end of the img source, not sure why. You forgot a # before the first main carousel in your ordered list, and the script at the end is unnecessary. I have done this for you here: https://jsfiddle.net/owq0e5vv/ and it seems to work fine.
I discovered the answer with a different question. After remembering that I had Angular Routing, I could narrow my search. Here's the answer:
Bootstrap carousel not working with angularjs

Using Bootstrap and working with Carousel

I am using Bootstrap and I am using Carousel to display a few pictures. I have it all in place and the first picture shows, however when you click the right and left Chevron's it doesn't take you to the next picture.
I am a little new to HTML / CSSl and definitely Bootstrap. Any help is appreciated.
Thanks. My Carousel code is below.
<div class="container-fluid">
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<!-- 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" role="listbox">
<div class="item active">
<img src="img/type.jpg" alt="typewriter">
<div class="carousel-caption">
<h3>The Header</h3>
<p>Header Text</p>
</div>
</div>
<div class="item">
<img src="img/pad.jpg" alt="scratchpad">
<div class="carousel-caption">
<h3>Second Header</h3>
<p>Second Text</p>
</div>
</div>
<div class="item">
<img src="img/screen.jpg" alt="monitor">
<div class="carousel-caption">
<h3>Third Header</h3>
<p>Third Text</p>
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
You probably forgot the bootstrap js file. can you include your header as well?
Make sure you have a link to bootstrap js (as well as css) in the html header
From the bootstrap site:
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

Bootstrap 3 carousel img/indicator not showing properly

I'm still very new to bootstrap/programming html in general as this is a new hobby. I have difficulty using the carousel properly and the next questions should take seconds to anybody I guess but I've been messing with codes for an hour and I can't still find why...
I copy/pasted the given code on bootstrap's site and filled the fields with proper items (used .svg for bg img, named the id differently but changed all the other ids too.). Maybe someone could tell me why the indicators/img are not fitted properly. i.e.: indicators are at the top left corner just like any other ordered list bullets and img show underneath them.
in head there is
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
---
<div class="container">
<!-- CAROUSEL -->
<div id="mon-carousel" class="carousel slide" data-ride="carousel" style="width: 450; height: 250; margin-left: auto; margin-right: auto;>
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#mon-carousel" data-slide-to="0" class="active"></li>
<li data-target="#mon-carousel" data-slide-to="1"></li>
<li data-target="#mon-carousel" data-slide-to="2"></li>
</ol>
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="img/solid-black.svg" alt="fond-gris" style="width: 450; height: 250;">
<div class="carousel-caption">
<h3>TEST CAPTION 1</h3>
<p>test</p>
</div>
</div>
<div class="item">
<img src="img/solid-black.svg" alt="fond-gris" style="width: 450; height: 250;">
<div class="carousel-caption">
<h3>TEST CAPTION 2</h3>
<p>test</p>
</div>
</div>
<div class="item">
<img src="img/solid-black.svg" alt="fond-gris" style="width: 450; height: 250;">
<div class="carousel-caption">
<h3>TEST CAPTION 2</h3>
<p>test</p>
</div>
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#mon-carousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#mon-carousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<!-- CAROUSEL -->
</div>
Your webpage is most probably missing the required bootstrap files.
Add this line to your <head>....</head> section:
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet">
And this line below your jQuery script line:
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
I realized that when I used style in the carousel's div it was the source of the problem... I guess I'll have to find an other way around to size the carousel.

Bootstrap Carousel Image Transition Slide Not Working

I am having an issue with the Bootstrap Carousel component, where the image is not sliding during transition.
Basically, the image seems to just get replaced (and does not slide) as can be seen from the Bootstrap site.
The other issue I am facing is that the image, when changing from one to the other seems to get overlaid on top of the left and right chevron icon controls.
I've scoured SO and found something similar:
Twitter Bootstrap Carousel Not Sliding
But the solution provided does not seem to apply for me as I am using the minified version of the Bootstrap JS which already has the transition function embedded.
Anyone came across a similar issue before?
As Leon has asked for some codes, I am practically perusing the same codes as posted in the Bootstrap website. Its just the issue has been the same where the image is not sliding in transition, but rather just getting invisible / visible and after a while where the slide gets visible, the chevron control seems to be behind the image (for a split second) and then gets on top of the image afterwards.
<!-- Carousel [START] -->
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- <ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
</ol> -->
<div class="carousel-inner" role="listbox">
<div class="item active">
<img src="assets/img/slider-images/header-images-01.jpg" alt="Slide 1" class="img-responsive">
</div>
<div class="item">
<img src="assets/img/slider-images/header-images-02.jpg" alt="Slide 2" class="img-responsive">
</div>
<div class="item">
<img src="assets/img/slider-images/header-images-03.jpg" alt="Slide 3" class="img-responsive">
</div>
</div>
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left glyphcolor" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right glyphcolor" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
<!-- Carousel [END] -->
UPDATE: The chevron icon also gets covered when I clicked on it to transition to the next slide.
Check out the css of div.item, if you have position: absolute / relative.
In other cases, try to create new document only your carousel, check if it works alone and if that's the case, your problem must be coming from earlier part of your code... Sometimes console wont show you what's wrong so you'll have to trace by yourself