How to stop jQuery from fading on nested div's? - html

var images = ["https://dummyimage.com/600x400/c4c4c4/0011ff.jpg&text=Image+1", "https://dummyimage.com/600x400/c4c4c4/ff0000.jpg&text=Image+2", "https://dummyimage.com/600x400/c4c4c4/fffb00.jpg&text=Image+3"];
$(function () {
var i = 0;
$("#dvImage").css("background-image", "url(" + images[i] + ")");
setInterval(function () {
i++;
if (i == images.length) {
i = 0;
}
$("#dvImage").fadeOut("slow", function () {
$(this).css("background-image", "url(" + images[i] + ")");
$(this).fadeIn("slow");
});
}, 2000);
});
p {padding: 75px 15px 15px 15px;text-align: center;}
#dvImage {background-size:cover;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="dvImage" style="width: 600px;height: 400px;">
<div id="text">
<p>How do I get this text to stop fading?</p>
</div>
</div>
I'm trying to create a background image rotator, that changes images and fades between each image. I've almost got it, but on the site that I'm using it, has a nested image and text, which is also fading. I only want it to fade the background images and not any of the nested div's.
I created an example here with a nested div (and paragraph tag with text).
https://jsfiddle.net/bobthegoat2001/3hrgua82/6/
Does anyone know how I could fix this? Any help would be great. Thanks!
<p style="display:none">I guess Stack Overflow wanted some code, so I just put some random here. The jsfiddle will make more since.</p>

You can use position absolute and then set z-index to -1 but you need to move nested div's out of your div #dvImage
var images = ["https://dummyimage.com/600x400/c4c4c4/0011ff.jpg&text=Image+1", "https://dummyimage.com/600x400/c4c4c4/ff0000.jpg&text=Image+2", "https://dummyimage.com/600x400/c4c4c4/fffb00.jpg&text=Image+3"];
$(function () {
var i = 0;
$("#dvImage").css("background-image", "url(" + images[i] + ")");
setInterval(function () {
i++;
if (i == images.length) {
i = 0;
}
$("#dvImage").fadeOut("slow", function () {
$(this).css("background-image", "url(" + images[i] + ")");
$(this).fadeIn("slow");
});
}, 2000);
});
p {padding: 75px 15px 15px 15px;text-align: center;}
#dvImage {position:absolute; z-index:-1;width:600px; height:400px; background-size:cover;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="dvImage"></div>
<div id="text">
<p>How do I get this text to stop fading?</p>
</div>

Related

Extend DIV width to screen width

Especially at low screen resolution, the screen can have an horizontal scrollbar. I want to expand div length to end of the scrollbar. So, width of div may bigger than 100%.
#content{border:2px solid red}
<body>
<div id="content">Test</div>
<div id="wrap">
NoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapText
</div>
</body>
I want to expand the content width to screen width.
How can I do this?
Thanks
#content{border:2px solid red}
body {
display:table;
}
<body>
<div id="content">Test</div>
<div id="wrap">
NoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapTextNoWrapText
</div>
</body>
Check it out the answer : do u want this?
https://jsfiddle.net/sesn/mh9juu1w/
I have added the function to calculate the text width
Here
$(function(){
$.fn.textWidth = function () {
$body = $('body');
$this = $(this);
$text = $this.text();
if($text=='') $text = $this.val();
var calc = '<div style="clear:both;display:block;visibility:hidden;"><span style="width;inherit;margin:0;font-family:' + $this.css('font-family') + ';font-size:' + $this.css('font-size') + ';font-weight:' + $this.css('font-weight') + '">' + $text + '</span></div>';
$body.append(calc);
var width = $('body').find('span:last').width();
$body.find('span:last').parent().remove();
return width;
};
wrapWidth = $('#wrap').textWidth();
$('#content').width(wrapWidth);
});
write the css for the #wrap div and set width 100%;
example
div#wrap {
width: 100%;
display: inline-block;
word-break: break-all;
}

Bootstrap 4 : align content inside cards deck

I have a deck of cards in my Bootstrap 4 page. I want to align these button to have a nicer look. How can we do that?
Here is an image:
And here is the demo : http://7freres.com/new
The table dosen't seem to works, as they are separated.
Thanls
You can make all the card-text elements to have the same height.
One way to do it can be through javascript. Write a function like this:
document.addEventListener("DOMContentLoaded", function(event) {
adjustCardTextHeights();
});
function adjustCardTextHeights() {
var heights = $(".card-text").map(function() {
return $(this).height();
}).get();
maxHeight = Math.max.apply(null, heights);
$(".card-text").height(maxHeight);
}
Or with jQuery:
$( document ).ready(function() {
adjustCardTextHeights();
});
function adjustCardTextHeights() {
var heights = $(".card-text").map(function() {
return $(this).height();
}).get();
maxHeight = Math.max.apply(null, heights);
$(".card-text").height(maxHeight);
}
This gets you:
You could set position: absolute; bottom: 20px; for the buttons and add an extra padding-bottom: 50px; to the card-block

Coding FadeIn/FadeOut for Slideshow

Currently I have a homepage for my website with a slideshow (4 pictures) as my background. The transition between images isn't very smooth and jumps from photo to photo. How can I create smoother transitions, is it possible to fade in and fade out?
All help greatly appreciated, thanks
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(document).ready(function(){
var header = $('body');
var backgrounds = new Array(
'url(http://urs2009.net/wp-content/uploads/2011/11/lights-of-city.jpg)'
, 'url(http://www.wallpaiper.com/wp-content/uploads/2014/06/amazing-wallpaper-hd-8.jpg)'
, 'url(http://hdwallpaperd.com/wp-content/uploads/2014/12/background-wallpaper-hd-1.jpg)'
, 'url(http://www.mrwallpaper.com/wallpapers/rocky-beach-hd.jpg)'
);
var current = 0;
function nextBackground() {
current++;
current = current % backgrounds.length;
header.css('background-image', backgrounds[current]);
}
setInterval(nextBackground, 5000);
header.css('background-image', backgrounds[0]);
});
</script>
Change your JS to this to make it more efficient. It would be better that you create a div that covers the entire body if you want the fade animations.:
HTML:
<body>
<div id="div1"></div>
</body>
CSS:
#div1
{
position:absolute;
top:0;
left:0;
bottom:0;
right: 0;
}
Javascript:
$(document).ready(function(){
var backgrounds = new Array(
'url(http://urs2009.net/wp-content/uploads/2011/11/lights-of-city.jpg)'
, 'url(http://www.wallpaiper.com/wp-content/uploads/2014/06/amazing-wallpaper-hd-8.jpg)'
, 'url(http://hdwallpaperd.com/wp-content/uploads/2014/12/background-wallpaper-hd-1.jpg)'
, 'url(http://www.mrwallpaper.com/wallpapers/rocky-beach-hd.jpg)'
);
var current = 0;
function nextBackground() {
if(current<backgrounds.length)
current++;
else
current=0;
$('#div1').fadeOut(function() {
$('#div1').css({background : backgrounds[current]) });
$('#div1').fadeIn(5000);
}, 5000);
}
setInterval(nextBackground, 5000);
});
It's difficult to fade background images as they have no opacity state. You can either have a -z-index div the same size as the body, and fade images in it behind the rest of your content, or you can float a mask over your background and dip to black (or another color) while you switch images. This is a simple method of doing the latter:
HTML:
<div id = "mask"></div>
CSS
#mask {
width:100%;
height:100%;
background:#000000;
top:0;
left:0;
position:absolute;
display:none;
}
jquery
$(document).ready(function(){
var header = $('body');
var backgrounds = new Array(
'url(http://urs2009.net/wp-content/uploads/2011/11/lights-of-city.jpg)'
, 'url(http://www.wallpaiper.com/wp-content/uploads/2014/06/amazing-wallpaper-hd-8.jpg)'
, 'url(http://hdwallpaperd.com/wp-content/uploads/2014/12/background-wallpaper-hd-1.jpg)'
, 'url(http://www.mrwallpaper.com/wallpapers/rocky-beach-hd.jpg)'
);
var current = 0;
function nextBackground() {
$('#mask').fadeTo(1000, 0.9, function() {
current++;
current = current % backgrounds.length;
header.css('background-image', backgrounds[current]);
})
$('#mask').fadeTo(500, 0);
}
setInterval(nextBackground, 5000);
header.css('background-image', backgrounds[0]);
});
You can obviously play with the color, timers and opacity to get the effect you want.
FIDDLE

positioning issue with hyper link

I have a div containing simple text. I should show only 3 lines of that text with showMore link. on clicking of showMore link I need to show all the text inside it with showLess link. Right now I am using overflow property to achieve this. But have a small problem that I should show "showMore" link immediately after the text. So how can I position the hyperlink inside the text?
My Code
jQuery(document).ready(function () {
jQuery('.showMore').click(function (event) {
var contentDiv = jQuery('.contentDiv');
contentDiv[0].style.height = 'auto';
jQuery(event.target).hide();
jQuery('.showLess').show();
});
jQuery('.showLess').click(function (event) {
var contentDiv = jQuery('.contentDiv');
var lineHeight = getLineHeight(contentDiv[0]);
contentDiv[0].style.height = lineHeight * 3 + 'px';
jQuery(event.target).hide();
jQuery('.showMore').show();
});
});
<!doctype html>
<html>
<body >
<div>
<div class = "contentDiv">
some content <br r1<br> r2<br> r3 <br> r4<br> r5
</div>
Show More
Show Less
</div>
</body>
</html>
.contentDiv a {
float : right;
}
.contentDiv {
overflow: hidden;
height:3.6em;
background:#ccc;
font-size : 12pt ;
font-family :Courier;
}
.showMore {
float: right;
}
.showLess {
position: relative;
float: right;
display : none;
margin-bottom : 5px
}
Just one of the million ways you could achieve this: fiddle
HTML
<div id="text">Lots of text..</div>
Show More
Show Less
CSS
#text {height:55px;overflow:hidden;}
#showless {display:none;}
jQuery
$('#showmore').on('click', function(e){
$('#text').css('overflow', 'visible').css('height', 'auto');
$('#showless').show();
$(this).hide();
e.preventDefault();
});
$('#showless').on('click', function(e){
$('#text').css('overflow', 'hidden').css('height', '55px');
$('#showmore').show();
$(this).hide();
e.preventDefault();
});
To position the 'show more' in the bottom right (as requested in your follow-up comment), put the <a> tag inside the div and position it absolutely. Fiddle
HTML
<div id="text">Lots of text..
Show More
</div>
Show Less
CSS
#text {height:55px;overflow:hidden;position:relative;}
#showmore {position:absolute;bottom:0;right:0;background:#fff;padding-left:10px;}
#showless{display:none;}

How to change font color in sticky nav bar when scrolling down?

I tried to change font color in sticky nav bar. When scrolling down, I want to change color of nav background red to another color and font color from white to black. I tried to change font color but it can't be changed.
body{
font-size:16px;
color:#FFFFFF;
}
header {
position: fixed;
top: 0;
left: 0;
right: 0;
background-color: #D02F32;
clear: both;
width: 100%;
min-height: 87px;
}
/* ==========================================================================
Navigation Style
========================================================================== */
nav{
/*background-color:#7E7E7E;*/
padding:2px;
width:800px;
margin:0 auto;
float:right;
margin-top:1%;}
nav ul{
list-style-type:none;
text-align:center;
font-size: 1em;}
nav li{
display:inline;
padding:6px;}
nav a:hover{
color:#82ED8E;}
nav a{
text-decoration:none;
color:#FFFFFF;
font-weight:bold;}
<body>
<header class="sticky">
<div class="wrapper">
<nav class="menu-top-container">
<ul id="top-nav" class="menu">
<li>Steps</li>
<li>questions</li>
<li>answers</li>
</ul>
</nav>
</div>
</header>
</body>
Here you go (this will change nav background color to blue and font color to black when you scroll more than 210px, and will revert background color back to red and font color to white if you go back up). In case, i use jQuery implement:
$(document).ready(function(){
var scroll_pos = 0;
$(document).scroll(function() {
scroll_pos = $(this).scrollTop();
if(scroll_pos > 210) {
$("nav").css('background-color', 'blue');
$("nav a").css('color', 'black');
} else {
$("nav").css('background-color', 'red');
$("nav a").css('color', 'white');
}
});
});
You can refer more follow the link:
jquery change background color user scroll
A solution can be achieved using JS to detect a scroll event. See the below example for a jQuery implementation.
I recommend using JS to add and remove a class to the header element instead of applying inline styles. This ensures your styles can be kept together in the CSS and will make life easier when changing styles in the future.
CSS
header.compact nav {
background-color:#7E7E7E;
}
JS
var header = $('header'),
minScroll = 50; // min distance to scroll before compacting header
// Listen to scroll event
$(window).scroll(function () {
var scrollTop = $(this).scrollTop();
if (scrollTop > minScroll) {
header.addClass('compact');
}
else {
header.removeClass('compact');
}
});
It is recommended to use some sort of throttling on the scroll handler to reduce the amount of times the code fires. A post on throttling can be found here.
Include the following Jquery link to your document:
<script src="http://code.jquery.com/color/jquery.color-2.1.0.js"></script>
And use the following Jquery Code to change the background color of Nav:
<script>
$(document).ready(function(){
var scroll_pos = 0;
var animation_begin_pos = 0; //where you want the animation to begin
var animation_end_pos = 1000; //where you want the animation to stop
var beginning_color = new $.Color( 'rgb(210,50,98)' ); //we can set this here, but it'd probably be better to get it from the CSS; for the example we're setting it here.
var ending_color = new $.Color( 'rgb(0,197,209)' ); ;//what color we want to use in the end
$(document).scroll(function() {
scroll_pos = $(this).scrollTop();
if(scroll_pos >= animation_begin_pos && scroll_pos <= animation_end_pos ) {
// console.log( 'scrolling and animating' );
//we want to calculate the relevant transitional rgb value
var percentScrolled = scroll_pos / ( animation_end_pos - animation_begin_pos );
var newRed = beginning_color.red() + ( ( ending_color.red() - beginning_color.red() ) * percentScrolled );
var newGreen = beginning_color.green() + ( ( ending_color.green() - beginning_color.green() ) * percentScrolled );
var newBlue = beginning_color.blue() + ( ( ending_color.blue() - beginning_color.blue() ) * percentScrolled );
var newColor = new $.Color( newRed, newGreen, newBlue );
//console.log( newColor.red(), newColor.green(), newColor.blue() );
$('header').animate({ backgroundColor: newColor }, 0);
} else if ( scroll_pos > animation_end_pos ) {
$('header').animate({ backgroundColor: ending_color }, 0);
} else if ( scroll_pos < animation_begin_pos ) {
$('header').animate({ backgroundColor: beginning_color }, 0);
} else { }
});
});
</script>
CSS:
header.scrolledHeader ul li a {
color: ###### //color you want
}
JS:
$(window).scroll( function(e) {
var scroll = $(this).scrollTop(); // getting the current position of Scroll
if( scroll > 20 ) {
$('header').addClass('scrolledHeader');
} else {
$('header').removeClass('scrollHeader');
}
})