I have a javascript slideshow. I added to my page and it works great. I'm not a javascript developer and got the solution online. However I need each image to link somewhere and despite how simple it should be, I cannot get it to work. When I add a tag the image disappears...can anyone help? Maybe add an additional config parameter to the javascript code where a URL could be entered?
(function($) {
"use strict";
$.fn.sliderResponsive = function(settings) {
var set = $.extend({
slidePause: 5000,
fadeSpeed: 800,
autoPlay: "off",
showArrows: "on",
hideDots: "on",
hoverZoom: "on",
titleBarTop: "off"
},
settings
);
var $slider = $(this);
var size = $slider.find("> div").length; //number of slides
var position = 0; // current position of carousal
var sliderIntervalID; // used to clear autoplay
// Add a Dot for each slide
$slider.append("<ul></ul>");
$slider.find("> div").each(function() {
$slider.find("> ul").append('<li></li>');
});
// Put .show on the first Slide
$slider.find("div:first-of-type").addClass("show");
// Put .showLi on the first dot
$slider.find("li:first-of-type").addClass("showli")
//fadeout all items except .show
$slider.find("> div").not(".show").fadeOut();
// If Autoplay is set to 'on' than start it
if (set.autoPlay === "on") {
startSlider();
}
// If showarrows is set to 'on' then don't hide them
if (set.showArrows === "on") {
$slider.addClass('showArrows');
}
// If hideDots is set to 'on' then hide them
if (set.hideDots === "on") {
$slider.addClass('hideDots');
}
// If hoverZoom is set to 'off' then stop it
if (set.hoverZoom === "off") {
$slider.addClass('hoverZoomOff');
}
// If titleBarTop is set to 'on' then move it up
if (set.titleBarTop === "on") {
$slider.addClass('titleBarTop');
}
// function to start auto play
function startSlider() {
sliderIntervalID = setInterval(function() {
nextSlide();
}, set.slidePause);
}
// on mouseover stop the autoplay
$slider.mouseover(function() {
if (set.autoPlay === "on") {
clearInterval(sliderIntervalID);
}
});
// on mouseout starts the autoplay
$slider.mouseout(function() {
if (set.autoPlay === "on") {
startSlider();
}
});
//on right arrow click
$slider.find("> .right").click(nextSlide)
//on left arrow click
$slider.find("> .left").click(prevSlide);
// Go to next slide
function nextSlide() {
position = $slider.find(".show").index() + 1;
if (position > size - 1) position = 0;
changeCarousel(position);
}
// Go to previous slide
function prevSlide() {
position = $slider.find(".show").index() - 1;
if (position < 0) position = size - 1;
changeCarousel(position);
}
//when user clicks slider button
$slider.find(" > ul > li").click(function() {
position = $(this).index();
changeCarousel($(this).index());
});
//this changes the image and button selection
function changeCarousel() {
$slider.find(".show").removeClass("show").fadeOut();
$slider
.find("> div")
.eq(position)
.fadeIn(set.fadeSpeed)
.addClass("show");
// The Dots
$slider.find("> ul").find(".showli").removeClass("showli");
$slider.find("> ul > li").eq(position).addClass("showli");
}
return $slider;
};
})(jQuery);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="why">
<div class="slider" id="slider1">
<div style="background-image:url(https://via.placeholder.com/150/0000FF); background-color: #e0e1dd;"></div>
<div style="background-image:url(https://via.placeholder.com/150/FF0000); background-color: #e0e1dd;"></div>
<div style="background-image:url(https://via.placeholder.com/150/00FF00); background-color: #e0e1dd;"></div>
<div style="background-image:url(https://via.placeholder.com/150/000000); background-color: #e0e1dd;"></div>
<i class="left" class="arrows" style="z-index:2; position:absolute;"><img src="cps-images/home-circle-left.png" id="lefticon"></i>
<i class="right" class="arrows" style="z-index:2; position:absolute;"><img src="cps-images/home-circle-right.png" id="righticon"></i>
</div>
<script src="cps-js/nbi-home-promo-slider.js"></script>
</div>
You can add this to the bottom of your page, before the </body> tag:
<script>
function visit(url){
window.open(url, '_self');
}
</script>
Then you can add your links to the div like this:
<div style="background-image:url(cps-images/home-slider1a.png); background-color: #e0e1dd;" onclick="visit('https://www.google.com')"></div>
Other solution would be to delete the background-images and just use regular "img" elements inside the "div" and then wrap them both inside "a" element.
In this case "img" should have the following styling:
img {
width: 100%;
height: 100%;
background-size: cover;
}
Related
I created a side menu, which displays and hides when clicking the menu button. Now, I would like that the menu closes when I click outside the menu, but I cannot figure out how to do it.
I have tried adding a clicklistener to the body, but this disables the menu completely. I also thougt about getting the ID of the clicked element anywhere in the body and close the menu if clickedElement != sideBar && sideBar.style = "200px", but I cannot get it to work. Can someone help me out here? I would like to find a solution without JQuery.
menuBtn.addEventListener("click", function () {
if (sideBar.style.width == "200px") {
sideBar.style.width = "0px";
setTimeout(function () {
menuItems.style.display = "none";
}, 1000);
} else {
sideBar.style.width = "200px";
menuItems.style.display = "block";
}
});
You can add an event listener to the parent container and check if the click's target is inside the element or not, something like:
document.addEventListener("click", (e) => {
if (box.contains(e.target)) {
result.innerHTML = "inside";
} else {
result.innerHTML = "outside";
}
});
#box {
width: 100px;
height: 100px;
background-color: dodgerblue;
}
<div id="box"></div>
<div id="result"></div>
I have a code that works perfectly to move elements from top, left, right or bottom sides, but seems to be, that it doesn't work with display property, here's my javascript code:
$('#icon-for-search').click(function () {
var targetValue;
if ($('#search-wrapper').css('top') == "0px") {
targetValue = '55px';
} else {
targetValue = '0px';
}
$("#search-wrapper").animate({
top: targetValue
}, 500);
});
I have a button with an id called "icon-for-search" and it toggles perfectly the top value of the #search-wrapper if it's clicked, but if I change it to display: block / none it doesn't work. Any particular reason? could someone explain me?
$('#icon-for-search').click(function () {
var targetValue;
if ($('#search-wrapper').css('display') == "none") {
targetValue = 'block';
} else {
targetValue = 'none';
}
$("#search-wrapper").animate({
display: targetValue
}, 500);
});
jQuery.animate works only with numeric CSS properties. You can just toggle element:
$('#icon-for-search').click(function () {
$('#search-wrapper').fadeToggle('slow');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="icon-for-search">Search Icon</div>
<div id="search-wrapper">Search Wrapper</div>
I was trying this jQuery example
(function ($) {
$(document).ready(function(){
// hide .navbar first
// $(".masthead").hide();
$(".masthead").css("background-color","inherit");
// fade in .navbar
$(function () {
$(window).scroll(function () {
// set distance user needs to scroll before we fadeIn navbar
if ($(this).scrollTop() > 600) {
$('.masthead').fadeIn();
$(".masthead").css("background-color","black");
} else if($(this).scrollTop === 0){
$('.masthead').fadeIn();
} else {
$('.masthead').fadeOut();
}
});
});
});
}(jQuery));
It shows the menu/navbar when I run the page and disapears when I start scrolling and after 700 pixels navbar is displayed again with black background, I expected it to fade in again after I come back to the top.
if($(this).scrollTop === 0){
$('.masthead').fadeIn();
}
But it have not worked. How do scrollTop() work then? I have also tried to set scrollTop < 10, but with no success. How do I make it work when I'm back at 10 pixels or zero?
You're comparing the function body to 0, not the actual results of the function (the scroll value), so this:
if($(this).scrollTop === 0){
$('.masthead').fadeIn();
}
should become this:
if($(this).scrollTop() === 0){
$('.masthead').fadeIn();
}
TL;DR
It's scrollTop vs. scrollTop().
I have some code in jQuery in which I want to make a switch animate on or off by clicking on a div. Here is the code. When I test it, it doesn't work. However if I remove toggle = true on line 7, it just works one way and I can't turn it back off.
$(document).ready(function () {
$("#switch").click(function () {
var toggle = false;
if (toggle == false) {
$("#circle").css("left", "27px");
$("#white_rect").attr("src", "green_rect.png");
toggle = true;
}
if (toggle == true) {
$("#circle").css("left", "1px");
$("#white_rect").attr("src", "white_rect.png");
toggle = false;
}
});
});
You need to declare the toggle variable outside of the click handler... else in every click call the variable will get reinitialized so the value of the variable will always be false.
$(document).ready(function () {
//declare it here
var toggle = false;
$("#switch").click(function () {
if (toggle == false) {
$("#circle").css("left", "27px");
$("#white_rect").attr("src", "green_rect.png");
toggle = true;
//also don't use a separate if block here as it will be affected by the execution of the above if block
} else {
$("#circle").css("left", "1px");
$("#white_rect").attr("src", "white_rect.png");
toggle = false;
}
});
});
$(document).ready(function () {
//declare it here
var toggle = false;
$("#switch").click(function () {
if (toggle) {
$("#circle").css("left", "1px");
$("#white_rect").attr("src", "white_rect.png");
} else {
$("#circle").css("left", "27px");
$("#white_rect").attr("src", "green_rect.png");
}
toggle = !toggle;
});
});
It is better to strictly divide appearance and logic. So use .classes and backgounded <div>s instead of <img>. Then you won't need any state variables and code shall be more simple.
HTML:
<div class="container">
<div class="switch off"></div>
</div>
CSS:
.container {
width:100%; height:100px; padding:40px; text-align:center;
}
.container .switch {
width:94px; height: 27px; display:inline-block; background-color:pink; cursor:pointer;
}
.container .switch.on {
background: url('http://squad.pw/tmp/img/01-on.png') no-repeat 0 0;
}
.container .switch.off {
background: url('http://squad.pw/tmp/img/01-off.png') no-repeat 0 0;
}
JS:
$('.switch').click(function() {
// Do visual logic
$(this).toggleClass('on');
$(this).toggleClass('off');
// Do business logic
window.toggle = !window.toggle;
});
Here is FIDDLE
Hi i want to have my side slider button to smoothly slide back to position if the user clicks outside of it.
If you wanna check it here is the jfiddle of it
as of now it doesnt go back to position when you click outside of it.
JS
$(document).ready(function () {
$('#slideleft .mybutton').click(function () {
var $lefty = $(this).parent();
$lefty.animate({
left: parseInt($lefty.css('left'), 10) == 0 ? -$lefty.outerWidth() + 31 : 0
});
});
});
Please try the below:
$(document).mouseup(function (e) {
var $lefty = $('#slideleft .mybutton').parent();
var container = $("#slideleft");
if (parseInt($lefty.css('left')) == 0) if (container.has(e.target).length === 0) {
animate();
}
});
Here is a working fiddle:
JSFiddle
Using some event.target trickery gives you this:
$(document).on('click', function (event) {
if (event.target !== $('#slideleft .mybutton')[0]) {
$('#slideleft .mybutton').hide(); //or slide however you want to.
}
});
If you notice this actually compares the actual HTML and not the jQuery object.
This is because .target give you back the actual HTML of what was clicked.
Adding this works:
$(document).click(function () {
var $lefty = $('#slideleft .mybutton').parent();
if(parseInt($lefty.css('left'), 10) == 0){
$lefty.animate({
left: -$lefty.outerWidth() + 31
});
}
});
Here is the fiddle