Slider not working properly at the last slide - javascript

I am currently learning Jquery and I'm a total newbie. Currently I was trying to make this custom slider work. What I am trying to achieve is, when someone clicks the prev or next buttons , nothing happens to the slider but the text written above it changes, i.e goes to the next <li> element. I wrote some code and it is working fine, but the problem comes when the slider is on the last child. When I click the next button the text disappears and now I cant even go back. I am pasting all the code I wrote here any help would be greatly appreciated and would help me to learn. THANKS A LOT!
HTML
<ul class="testimonial-text">
<li class="test-current">
<p>“Sagittis risus nec venenatis. Ut laoreet iaculis massa et feugiat. Cras elementum quamvitae magna as elementum quamvitae magna porttitor in fermentum setlat ul.”</p>
<h4>Michel Buble & Tinta turner</h4>
</li>
<li>
<p>“Sagittis risus nec venenatis. Ut laoreet iaculis massa et feugiat. Cras elementum quamvitae magna as elementum quamvitae magna porttitor in fermentum setlat ul.”</p>
<h4>Rick Armstrong & Ashley Tist</h4>
</li>
<li>
<p>“Sagittis risus nec venenatis. Ut laoreet iaculis massa et feugiat. Cras elementum quamvitae magna as elementum quamvitae magna porttitor in fermentum setlat ul.”</p>
<h4>Mike Tran & Kimse Tricks</h4>
</li>
<li>
<p>“Sagittis risus nec venenatis. Ut laoreet iaculis massa et feugiat. Cras elementum quamvitae magna as elementum quamvitae magna porttitor in fermentum setlat ul.”</p>
<h4>Michel Buble & Tinta turner</h4>
</li>
<li>
<p>“Sagittis risus nec venenatis. Ut laoreet iaculis massa et feugiat. Cras elementum quamvitae magna as elementum quamvitae magna porttitor in fermentum setlat ul.”</p>
<h4>Michel Buble & Tinta turner</h4>
</li>
</ul>
the buttons
<a class="testimonials-prev" ></a>
<a class="testimonials-next" ></a>
CSS
ul.testimonial-text{
list-style: none;
padding-top: 20px;
}
ul.testimonial-text li{
display: none;
}
ul.testimonial-text li.test-current{
display: block;
}
ul.testimonial-text li p{
text-align: center;
font-size: 18px;
color: #7c7c7c;
margin-bottom: 0px;
}
ul.testimonial-text li h4{
text-align: center;
text-transform: uppercase;
font-size: 16px;
}
ul.testimonial-images{
list-style: none;
}
ul.testimonial-images li{
display: inline-block;
margin-left: 16px;
opacity: 0.6;
}
ul.testimonial-images li:first-child{
display: inline-block;
margin-left: 0px;
}
a.testimonials-prev{
background: url('../images/icons/test-icons.png') no-repeat;
height: 80px;
width: 40px;
display: block;
margin-left: 10px;
cursor: pointer;
}
a.testimonials-next{
background: url('../images/icons/test-icons.png') no-repeat top right;
height: 80px;
width: 40px;
display: block;
cursor: pointer;
}
JQUERY / JAVASCRIPT
<script type="text/javascript">
$(document).ready(function() {
$("a.testimonials-next").click(function() {
$("ul.testimonial-text li.test-current").fadeOut('slow', function() {
$("ul.testimonial-text li.test-current").next().fadeIn('slow');
$("ul.testimonial-text li.test-current").next().addClass('test-current');
$("ul.testimonial-text li.test-current:first").removeClass('test-current');
});
});
$("a.testimonials-prev").click(function() {
$("ul.testimonial-text li.test-current").fadeOut('slow', function() {
$("ul.testimonial-text li.test-current").prev().fadeIn('slow');
$("ul.testimonial-text li.test-current").prev().addClass('test-current');
$("ul.testimonial-text li.test-current:last").removeClass('test-current');
});
});
});
</script>
I'm very sorry if the code is sloppy. Thanks alot!

Try this :-
$("a.testimonials-next").click(function() {
$("ul.testimonial-text li.test-current").fadeOut('slow', function() {
var index = $("ul.testimonial-text li).index($(this));
if(index == $("ul.testimonial-text li).length - 1){
var $next = $("ul.testimonial-text li:first");
}
else {
var $next = $(this).next();
}
$(this).removeClass('test-current');
$next.addClass('test-current').fadeIn('slow');
});
});
$("a.testimonials-prev").click(function() {
$("ul.testimonial-text li.test-current").fadeOut('slow', function() {
var index = $("ul.testimonial-text li).index($(this));
if(index == 0){
var $prev = $("ul.testimonial-text li:last");
}
else {
var $prev = $(this).prev();
}
$(this).removeClass('test-current');
$prev.addClass('test-current').fadeIn('slow');
});
})
;

#Ragzor use this code for your problem
<html>
<head>
<title>Testing</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<style type="text/css">
ul.testimonial-text{
list-style: none;
padding-top: 20px;
}
ul.testimonial-text li{
display: none;
}
ul.testimonial-text li.test-current{
display: block;
}
ul.testimonial-text li p{
text-align: center;
font-size: 18px;
color: #7c7c7c;
margin-bottom: 0px;
}
ul.testimonial-text li h4{
text-align: center;
text-transform: uppercase;
font-size: 16px;
}
ul.testimonial-images{
list-style: none;
}
ul.testimonial-images li{
display: inline-block;
margin-left: 16px;
opacity: 0.6;
}
ul.testimonial-images li:first-child{
display: inline-block;
margin-left: 0px;
}
a.testimonials-prev{
background: url('../images/icons/test-icons.png') no-repeat;
height: 80px;
width: 40px;
display: block;
margin-left: 10px;
cursor: pointer;
}
a.testimonials-next{
background: url('../images/icons/test-icons.png') no-repeat top right;
height: 80px;
width: 40px;
display: block;
cursor: pointer;
}
</style>
</head>
<body>
<a class="testimonials-prev" >Prev</a>
<ul class="testimonial-text">
<li class="test-current">
<p>1 “Sagittis risus nec venenatis. Ut laoreet iaculis massa et feugiat. Cras elementum quamvitae magna as elementum quamvitae magna porttitor in fermentum setlat ul.”</p>
<h4>Michel Buble & Tinta turner</h4>
</li>
<li>
<p>2 “Sagittis risus nec venenatis. Ut laoreet iaculis massa et feugiat. Cras elementum quamvitae magna as elementum quamvitae magna porttitor in fermentum setlat ul.”</p>
<h4>Rick Armstrong & Ashley Tist</h4>
</li>
<li>
<p>3 “Sagittis risus nec venenatis. Ut laoreet iaculis massa et feugiat. Cras elementum quamvitae magna as elementum quamvitae magna porttitor in fermentum setlat ul.”</p>
<h4>Mike Tran & Kimse Tricks</h4>
</li>
<li>
<p>4 “Sagittis risus nec venenatis. Ut laoreet iaculis massa et feugiat. Cras elementum quamvitae magna as elementum quamvitae magna porttitor in fermentum setlat ul.”</p>
<h4>Michel Buble & Tinta turner</h4>
</li>
<li>
<p>5 “Sagittis risus nec venenatis. Ut laoreet iaculis massa et feugiat. Cras elementum quamvitae magna as elementum quamvitae magna porttitor in fermentum setlat ul.”</p>
<h4>Michel Buble & Tinta turner</h4>
</li>
</ul>
<a class="testimonials-next" >Next</a>
<script type="text/javascript">
$(document).ready(function() {
$("a.testimonials-next").click(function() {
$("ul.testimonial-text li.test-current").fadeOut('slow', function() {
var index = $("ul.testimonial-text li").index($(this));
if(index == $("ul.testimonial-text li").length - 1){
var $next = $("ul.testimonial-text li:first");
}
else {
var $next = $(this).next();
}
$(this).removeClass('test-current');
$next.addClass('test-current').fadeIn('slow');
});
});
$("a.testimonials-prev").click(function() {
$("ul.testimonial-text li.test-current").fadeOut('slow', function() {
var index = $("ul.testimonial-text li").index($(this));
if(index == 0){
var $prev = $("ul.testimonial-text li:last");
}
else {
var $prev = $(this).prev();
}
$(this).removeClass('test-current');
$prev.addClass('test-current').fadeIn('slow');
});
})
});
</script>
</body>
</html>

Maybe you could add an if statement before the fade that would check to see if the li is the first one? (you would do similar for the next button as well).
Here's a JSFiddle working: http://jsfiddle.net/JUveT/
Example:
$("a.testimonials-prev").click(function() {
if($("ul.testimonial-text li.test-current").prev().length){
$("ul.testimonial-text li.test-current").fadeOut('slow', function() {
$("ul.testimonial-text li.test-current").prev().fadeIn('slow');
$("ul.testimonial-text li.test-current").prev().addClass('test-current');
$("ul.testimonial-text li.test-current:last").removeClass('test-current');
});
}
I got the code for the condition from here:
How to check if the element is not the first-child?

#Ragzor why don't you use Carousel give here, its easy, reliable and works perfectly fine. I have used it many times.

The script hides the active li.current-test regardless if a next/prev element exists. When you reach the last element, a next element naturally does not exist, but the script still hides the current active element regardless. You need to check first if a next element actually exists:
$("a.testimonials-next").click(function() {
// Switch to the next element, if it exists
if ($("ul.testimonial-text li.test-current").next().length) {
$("ul.testimonial-text li.test-current").fadeOut('slow', function() {
$("ul.testimonial-text li.test-current").next().fadeIn('slow');
$("ul.testimonial-text li.test-current").next().addClass('test-current');
$("ul.testimonial-text li.test-current:first").removeClass('test-current');
}
});
Now, that being said, your code could benefit greatly from assigning jQuery objects to variables and chaining of methods, that would make your code more readable and faster. Consider this snippet in regard to the one above:
$("a.testimonials-next").click(function() {
var current_element = $("ul.testimonial-text li.test-current"),
next_element = current_element.next();
// Switch to the next element, if it exists
if (next_element.length) {
current_element.removeClass('test-current').fadeOut('slow', function() {
next_element.addClass('test-current').fadeIn('slow');
});
});

Related

Blur the entire html when the sub-menu is open

I created a submenu using HTML and CSS. The elements of the submenu open when hovered on the elements of nav-list. I need help figuring out how to blur the HTML's main content when the nav-list hovers and sub-menu is open. When open, the submenu will have a picture below it and multiple pictures and classes further down. The current code does blur the HTML, but the problem is, to blur the entire HTML, I need to add each class below the submenu to CSS individually.
nav {
display: inline-flex;
width: 100%;
}
.nav-list {
display: flex;
width: 100%;
margin-top: .7rem;
padding-left: 1.1rem;
}
.nav-list li {
position: relative;
}
.nav-list>li>a {
color: black;
display: block;
font-size: 1rem;
padding: 1.3rem 1rem;
text-transform: uppercase;
}
.sub-menu {
display: flex;
position: absolute;
box-sizing: border-box;
background-color: black;
visibility: hidden;
top: 3.89rem;
left: -4rem;
width: 82.5rem;
height: 35rem;
z-index: 5000;
}
.sub-menu a {
position: relative;
top: 2rem;
color: white;
font-size: 1.1rem;
font-weight: 200;
padding: 3rem 40px 0 40px;
}
.nav-list>li:hover .sub-menu {
visibility: visible;
}
.nav-list>li:hover>a::after {
width: 100%;
}
/* CSS for blur effect */
.blurred::before {
content: "";
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.8);
filter: blur(5px);
/* adjust the blur amount */
z-index: 900;
visibility: hidden;
}
<div class="main" id="navbar">
<div class="logo">
XYZ<br>123
</div>
<nav class="menu">
<ul class="nav-list">
<li>
Men
<ul class="sub-menu">
<li>shirts </li>
</ul>
</li>
</ul>
</nav>
</div>
<div class="main">
<img></img>
<img></img>
<img></img>
<img></img>
</div>
<footer>
1
</footer>
<script>
var subMenu = document.querySelector('.sub-menu');
var main = document.querySelectorAll('main');
subMenu.addEventListener('mouseover', function() {
main.forEach(function(elem) {
elem.classList.add('blurred');
});
});
subMenu.addEventListener('mouseout', function() {
main.forEach(function(elem) {
elem.classList.remove('blurred');
});
});
</script>
You only need to add the blurred class to the main container div, all content will be blurred in it.
Demo for blur on hover over hamburger (you can change that to blur only on sub-menu hover). There is a 2 sec delay on mouse-out to that you have time to scroll down to see the blurred image:
const hamburger = document.getElementById('hamburger');
const main = document.getElementById('mainContainer');
hamburger.addEventListener('mouseover', function() {
main.classList.add('blurred');
});
hamburger.addEventListener('mouseout', function() {
setTimeout(() => main.classList.remove('blurred'), 2000);
});
.blurred {
-webkit-filter: blur(5px);
-moz-filter: blur(5px);
-o-filter: blur(5px);
-ms-filter: blur(5px);
filter: blur(5px);
background-color: #eee;
}
<div class="main" id="navbar">
NAVBAR mouse over to blur ==> <span id="hamburger">☰</span>
<hr />
</div>
<div class="main" id="mainContainer">
<h2>Lorem Ipsum</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam iaculis blandit felis vel hendrerit. Morbi nunc augue, pellentesque eu laoreet in, bibendum nec sem.</p>
<ul><li>In eu libero eget nulla laoreet aliquet in quis augue.</li><li>Praesent vulputate leo tellus, quis eleifend purus viverra vitae.</li><li>Phasellus eget augue venenatis, facilisis nulla sed, consequat lorem.</li></ul>
<p>Duis sapien diam, dignissim quis imperdiet eget, hendrerit nec nulla. Nam nisi purus, ultrices nec fringilla vitae, interdum vitae nunc.</p>
<p><img src="https://via.placeholder.com/500x90/000000/FFFFFF.png?text=This+is+an+embedded+image" /></p>
<p>Integer lobortis tellus a orci ultrices, at varius risus fermentum. Maecenas maximus elit sit amet varius facilisis. Morbi molestie rutrum eros, id molestie dui maximus in. Etiam fermentum felis eu vehicula euismod.</p>
</div>
<footer>
<hr />
FOOTER
</footer>

Showing error 'Uncaught TypeError: $(...).unhighlight is not a function', Even after loading the Highlight.js plugin

I'm getting a console error
Uncaught TypeError: $(...).unhighlight is not a function'
Even after loading the Highlight.js plugin. Please find the attached HTML code.
var userGuide = {
id: null,
initialize: function() {
//debugger
userGuide.initializeUSerGuide();
},
initializeUSerGuide: function() {
var data1 = '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam facilisis lectus et libero faucibus, convallis aliquam ante pharetra. Sed efficitur convallis lectus, in egestas tellus ornare tristique. Quisque blandit dolor non convallis convallis. Etiam facilisis velit ac dolor porta gravida. In hac habitasse platea dictumst. Ut suscipit efficitur diam, non rutrum mi posuere venenatis. Sed ullamcorper, ligula vitae convallis lacinia, lectus nibh scelerisque lacus, sed molestie lorem massa non orci. Duis sit amet justo magna. Nam malesuada neque et lectus scelerisque, sit amet rhoncus arcu blandit. Ut congue et est elementum bibendum. Morbi ligula mi, iaculis vitae pharetra ut, tristique eu arcu. Sed at dolor et magna pellentesque ornare. In urna sem, semper in augue id, tempus hendrerit augue. Maecenas vel porta justo. Aliquam porttitor elit eu neque hendrerit maximus.</p><p>Pellentesque mollis est vitae ornare elementum. Praesent sed neque ut lorem vestibulum malesuada eget a sem. Maecenas quam dui, ornare vitae orci nec, commodo vehicula nulla. Donec fermentum nisi eu mollis porttitor. Curabitur semper tortor eu magna cursus molestie. Praesent at pellentesque sapien, eget mollis quam. In vitae fringilla nisi, a ullamcorper sapien. Nunc ac enim porta, molestie leo quis, tristique sapien. Suspendisse in elit id urna faucibus rutrum.</p><p>Praesent convallis laoreet'
$("#ListView").html(data1);
}
}
* {
box-sizing: border-box;
}
body {
margin: 0;
line-height: 1.7;
font-family: 'Roboto';
background-color: #fafafa;
}
.finder {
position: fixed;
top: -50px;
right: 0;
left: 0;
z-index: 999;
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
height: 50px;
background: #fff;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
border-bottom: 1px solid #e5e5e5;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
padding: 5px;
transition: top 0.15s ease-out;
}
.finder.active {
top: 0;
}
.finder-input {
flex-grow: 1;
width: 0;
height: 40px;
border: none;
padding: 8px;
outline: none !important;
}
.finder-input.not-found {
color: #e7353f;
}
.btn-finder {
width: 40px;
height: 40px;
text-align: center;
padding: 0;
}
.finder-input,
.btn-finder:not(:last-child) {
margin-right: 5px;
}
.finder-count {
display: -webkit-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
flex-direction: column;
justify-content: center;
color: #888;
border-right: 1px solid #e5e5e5;
margin-right: 5px;
padding-right: 20px;
white-space: nowrap;
}
.btn-finder,
.finder-count {
flex-shrink: 0;
flex-grow: 0;
}
.highlight {
background: #ff8;
}
.highlight.active {
background: #ffd71b;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://johannburkard.de/resources/Johann/jquery.highlight-5.js"></script>
<script src="https://github.com/flesler/jquery.scrollTo/blob/master/jquery.scrollTo.js"></script>
<script type="text/javascript">
/**
* jQuery Finder Plugin
* #author Danny McGee
* #version 1.0
* github.com/dannymcgee
*
* Copyright 2018 Danny McGee
* Released under the MIT License
* https://opensource.org/licenses/MIT
*/
$(document).ready(function() {
$(finder.activator).click(function() {
finder.activate();
});
$(document).mousedown(function(event) {
if (event.which === 1) {
switch ($(event.target).attr('id') || $(event.target).parents().attr('id')) {
case 'finderClose':
finder.closeFinder();
break;
case 'finderPrev':
finder.prevResult();
break;
case 'finderNext':
finder.nextResult();
break;
default:
return true;
}
}
});
});
const finder = {
activator: '[data-finder-activator]',
content: '[data-finder-content]',
wrapper: '[data-finder-wrapper]',
scrollOffset: () => $(finder.wrapper).data('finderScrollOffset'),
activate: () => {
if (!$('#finder').length) {
finder.createFinder();
}
setTimeout(function() {
$('#finder').addClass('active');
$('#finderInput').focus();
if ($('#finderInput').val()) {
finder.findTerm($('#finderInput').val());
}
$('#finderInput').on('input', function() {
finder.findTerm($(this).val());
});
}, 50);
},
createFinder: () => {
const finderElem = $('<div />')
.attr({
'id': 'finder',
'class': 'finder'
})
.prependTo(finder.wrapper);
const input = $('<input />')
.attr({
'id': 'finderInput',
'type': 'text',
'class': 'finder-input',
})
.appendTo(finderElem);
const prev = $('<button />')
.attr({
'id': 'finderPrev',
'class': 'btn btn-finder btn-finder-prev',
})
.appendTo(finderElem);
const prevIcon = $('<i />')
.attr({
'class': 'fas fa-angle-up',
})
.appendTo(prev);
const next = $('<button />')
.attr({
'id': 'finderNext',
'class': 'btn btn-finder btn-finder-next',
})
.appendTo(finderElem);
const nextIcon = $('<i />')
.attr({
'class': 'fas fa-angle-down',
})
.appendTo(next);
const close = $('<button />')
.attr({
'id': 'finderClose',
'class': 'btn btn-finder btn-finder-close',
})
.appendTo(finderElem);
const closeIcon = $('<i />')
.attr({
'class': 'fas fa-times',
})
.appendTo(close);
},
closeFinder: () => {
$('#finder').removeClass('active');
$(finder.content).unhighlight();
},
resultsCount: 0,
currentResult: 0,
findTerm: (term) => {
// highlight results
$(finder.content).unhighlight();
$(finder.content).highlight(term);
// count results
finder.resultsCount = $('.highlight').length;
if (finder.resultsCount) {
// there are results, scroll to first one
finder.currentResult = 1;
finder.scrollToCurrent();
} else {
// no results
finder.currentResult = 0;
}
// term not found
if (!finder.resultsCount && term) {
$('#finderInput').addClass('not-found');
} else {
$('#finderInput').removeClass('not-found');
}
finder.updateCurrent();
},
scrollToCurrent: () => {
let scrollingElement;
let i = finder.currentResult - 1;
$('.highlight').removeClass('active');
$(`.highlight:eq(${i})`).addClass('active');
let offsetTop = -100;
if (finder.scrollOffset() !== null) {
offsetTop = finder.scrollOffset() * -1;
}
$(finder.wrapper).scrollTo('.highlight.active', {
offset: {
left: 0,
top: offsetTop,
},
});
},
prevResult: () => {
if (finder.resultsCount) {
if (finder.currentResult > 1) {
finder.currentResult--;
} else {
finder.currentResult = finder.resultsCount;
}
finder.scrollToCurrent();
}
finder.updateCurrent();
},
nextResult: () => {
if (finder.resultsCount) {
if (finder.currentResult < finder.resultsCount) {
finder.currentResult++;
} else {
finder.currentResult = 1;
}
finder.scrollToCurrent();
}
finder.updateCurrent();
},
updateCurrent: () => {
if ($('#finderInput').val()) {
if (!$('#finderCount').length) {
const countElem = $('<span />')
.attr({
'id': 'finderCount',
'class': 'finder-count',
})
.insertAfter('#finderInput');
}
setTimeout(function() {
$('#finderCount').text(finder.currentResult + ' / ' + finder.resultsCount);
}, 50);
} else {
$('#finderCount').remove();
}
},
}
</script>
<script type="text/javascript">
$(document).ready(function() {
userGuide.initialize();
});
</script>
<div class="container-fluid page-content">
<div class="app" data-finder-wrapper data-finder-scroll-offset="175">
<button class="finder-activator" data-finder-activator>
Search
</button>
<div class="content" data-finder-content id="ListView">
</div>
</div>
</div>
What am I doing wrong here?

How do I add a JavaScript event handler so that when the user moves the mouse cursor onto the content element a timeout timer starts?

Here is what I am trying to do,
I want to add a JavaScript event handler so that when the user moves the mouse cursor onto the content element, a timeout timer is started that will set the opacity of the payWall element to 1.0 – three second later.
Then I want to dd another JavaScript event handler so that when the user clicks the subscribe button, an alert box appears with the message “Subscribing now.”
When the alert is OK-ed, the payWall slides down the page and out of sight. I think I will need to set an interval timer so that the payWall moves down like 2 pixels every 30 milliseconds.
I am not sure how to do it, I tried my best, but if someone can please help me, I would really appreciate it.
function init()
{
document.getElementById("subscribe").onclick = function()
{
}
}
window.onload=init;
* {
margin: 0;
box-sizing: border-box;
}
body {
font: 1em Verdana, sans-serif;
background-color: antiquewhite;
}
h2, h4 {
text-align: center;
}
#header {
border-bottom: 2px solid black;
font-size: 2.5em;
padding: 0.5em 0;
height: 100px;
}
#footer {
border-top: 2px solid black;
padding: 1em 0;
}
#header, #footer {
text-align: center;
background-color: #CCC;
}
#leftnav, #rightnav {
position: absolute;
width: 20%;
padding-top: 3em;
}
#rightnav{
left: 80%;
}
#wrapper {
background-color: dodgerblue;
overflow: hidden;
}
#content div {
border: 1px solid black;
border-radius: 10px;
padding: 0.5em;
margin-bottom: 10px;
background-color: #ccc;
}
#content div:last-child {
margin-bottom: 0px;
}
#content div:hover {
border-color: dodgerblue;
background-color: white;
}
#content {
padding: 0.5em;
margin: 0 20%;
border-left: 2px solid black;
border-right: 2px solid black;
background-color: white;
}
/* ---------------------------------------------------------*/
#payWall {
background-color : darkseagreen;
font-size: 2em;
opacity: 0.5;
}
<body>
<div id="header">
The Header
</div>
<div id="wrapper"> <!-- Can be used to apply bg colour -->
<div id="leftnav">
<h4> Left</h4>
</div>
<div id="rightnav">
<h4> Right</h4>
</div>
<div id="content">
<div>
<h2> Article 1 </h2>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Suspendisse ultricies condimentum velit vel scelerisque.
</p>
</div>
<div>
<h2> Article 2 </h2>
<p>
Mauris sagittis aliquam odio vitae pulvinar.
Suspendisse id dolor nibh, sed consectetur sem.
Phasellus lacinia laoreet sem, ac ultrices libero lobortis quis.
Morbi accumsan tempus neque, sed varius lectus molestie imperdiet.
Vivamus porttitor facilisis nunc, sed feugiat quam adipiscing ac.
</p>
</div>
<div>
<h2> Article 3 </h2>
<p>
Proin ultrices lectus vel orci lacinia a iaculis nibh hendrerit.
Mauris sagittis aliquam odio vitae pulvinar.
Suspendisse id dolor nibh, sed consectetur sem.
Phasellus lacinia laoreet sem, ac ultrices libero lobortis quis.
Morbi accumsan tempus neque, sed varius lectus molestie imperdiet.
Vivamus porttitor facilisis nunc, sed feugiat quam adipiscing ac.
</p>
</div>
</div> <!-- end of content -->
</div> <!-- end of wrapper -->
<div id="footer">
<h3>
The End
</h3>
<div id="payWall">
For further access please subscribe here. <br>
<button id="subscribe"> Subscribe</button>
</div>
</div>
</body>
First:
Use <div onmouseenter='setTimeout(doStuff, 3000)'> for a three second delay when the cursor enters the div
In JS:
Add a function, doStuff, which has the following code:
document.getElementById('payWall').style.opacity = '1';
document.getElementById('subscribe').onclick = function(){
alert("Subscribing now");
vanishPayWall();
}
One problem is that the payWall would just keep moving down the page. If you wanted it to disappear, you'd want to put overflow-y: hidden on the payWall, and shorten the paywall by 2px every time as well as setting the upper margin to 2px higher. Maybe something like
function vanishPayWall() {
var key = window.setInterval(function(){
var pw = document.getElementById('payWall');
pw.style.height = String(Number(pw.style.height.slice(-2)) - 2) + 'px';
pw.style.marginTop = String(Number(pw.style.height.slice(-2)) + 2) + 'px';
}, 25)
setTimeout(function(){
clearInterval(key)
}, Number(pw.style.height.slice(-2)) * 12.5)
}
Not absolutely sure this will work, but it should help out a bit.

Custom Popup Window

I have created a design for my web page.
The issue I face is I am unsure how to create a smaller window popup, with my design.
The window will open up once I click on an image and display it's image on it with it's specific information.
I am unsure if I need some kind of JavaScript object to save their information.
This is kind of where I have started but I am lost.
I need the image based on which is clicked.
So to display the object I need to display it based on which image I click and unsure how.
Below is my design.
function Cupcakes(type, name, description, Price, cost, image){
this.type = type; //create an instant of object
this.name = name;
this.description = description;
this.Price = Price;
this.cost = cost;
thi.image = image;
this.displayInfo = function(){
var info ="<div class='divCell1' id = 'line1'>";
info += this.name + "</div><div class='divCell2' id = 'line2'>";
info += this.description + "</div><div class='divCell3' id = 'line3'>";
info += this.Price + "<div>Price <br>";
info += this.cost + "</div><div class = 'divCell4' id='line4'>";
info += this.image + "</div>";
return info;
}
}
// define an array to store products
var product_list = [];
var cart = [];
var cost = "Half Dozen: $7.00 <br> Dozen: $12.50 <br> Party Size [20 cupcakes]: $18.00"
var desc1 = ' Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus orci elit, lobortis nec neque in, condimentum gravida velit. Suspendisse maximus nisl et vehicula placerat. Sed elit turpis, venenatis sit amet tortor vel, interdum cursus mi.';
var image = " ";
var product = new Products('cupcake','Winter Festival', desc1, 15.99);
product_list.push(product);
Pop-ups are blocked on browsers by default. Use a div with position:fixed instead. You can hide/show it using display:none / display:inline-block
function show(){
var stats = document.getElementById("aa").style.display;
if (stats == "none"){
document.getElementById("aa").style.display = "inline-block";
} else {
document.getElementById("aa").style.display = "none";
}
}
body {
height: 100%;
background: honeydew;
}
#aa {
font-weight: bold;
position:fixed;
left:0;
right:0;
margin: auto;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
width: 100px;
height: auto;
padding: 15px;
background: gold;
text-align: center;
box-sizing: content-box;
border: 4px dashed black;
}
button {
margin-right: 10px;
float: left;
color: white;
background: crimson;
padding: 15px;
}
#text {
text-align: justify;
-webkit-column-count: 3; /* Chrome, Safari, Opera */
-moz-column-count: 3; /* Firefox */
column-count: 3;
}
<div id=text><button onClick=show()>click</button> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam nibh. Nunc varius facilisis eros. Sed erat. In in velit quis arcu ornare laoreet. Curabitur adipiscing luctus massa. Integer ut purus ac augue commodo commodo. Nunc nec mi eu justo tempor consectetuer. Etiam vitae nisl. In dignissim lacus ut ante. Cras elit lectus, bibendum a, adipiscing vitae, commodo et, dui. Ut tincidunt tortor. Donec nonummy, enim in lacinia pulvinar, velit tellus scelerisque augue, ac posuere libero urna eget neque. Cras ipsum. Vestibulum pretium, lectus nec venenatis volutpat, purus lectus ultrices risus, a condimentum risus mi et quam. Pellentesque auctor fringilla neque. Duis eu massa ut lorem iaculis vestibulum. Maecenas facilisis elit sed justo. Quisque volutpat malesuada velit.</div>
<div id=aa style="display:none">CONTENT</div>
Pure CSS solution with undercover checkbox:
html {
width: 100%;
height: 100%;
background: lavender;
text-align: center;
font-family: arial, sans-serif;
}
input {
display: none;
}
#target {
display: none;
}
#click:checked ~ label > #target {
position: fixed;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
left: 0;
right: 0;
margin: auto;
display: inline-block;
height: 80%;
width: 80%;
background: url('http://i.imgur.com/bv80Nb7.png');
background-repeat: no-repeat;
background-size: 100% 100%;
outline: 6px double teal;
}
.item {
position: fixed;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
left: 0;
right: 0;
margin: auto;
cursor: pointer;
user-select: none;
-webkit-user-select: none;
}
#warning {
position: fixed;
top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);
left:0;
right:0;
margin: auto;
}
<input type="checkbox" id="click" name="click" value="click" />
<label for="click">
<p class="item"><b>CLICK HERE</b></p>
<div id=target class="item"><h1 id=warning>THE POP-UP CONTENT</h1></div>
</label>
You question is very very confusing.
Basic javascript (considering jQuery) for you:
You can save the information of each cupcake on it element. Like this:
<div class="cupcake" data-price="Half Dozen: $7.00..." data-description="My description">
[...]
</div>
<div class="cupcake" data-price="Half Dozen: $12.00..." data-description="My description 2">
[...]
</div>
and in jquery:
$(".cupcake").on("click", function(){
var price = $(this).data("price"); //Getting the price information
var desc = $(this).data("price"); //Getting the description information
//Here you can use all information, call functions and show your small popup
});
Based in what div you click, the variable price and desc will receive the correct information.
This is a little bit useful for you ?
If you want, please edit your question and try to be more specific, because I don't get your real problem. (Maybe it's my english)

Wordpress + JQuery UI Tabs + Dynamically changing Width of Tabs

I'm trying to setup a tabbed pane (via jQuery) on one of my pages in a WordPress site. I have the tabs working and for the most part, it looks how I want it at this stage. There's one issue though - setting the width of the tabs so that each tab's span is long enough that all of them combined is equal to the space of the pane. Now this normally wouldn't be too hard, but the thing is I don't know how many tabs will be there in the end (my client is unsure).
Here's the CSS I'm using via Jetpack's Custom CSS functionality:
.ui-tabs-nav {
font-size: larger;
font-weight: bold;
background: none;
height: 30px;
/*To stop nav block scaling of tab size*/
}
.ui-tabs .ui-tabs-nav {
margin: 0;
padding: .2em .2em 0;
border: none;
background: none;
}
.ui-tabs .ui-tabs-nav li {
display: inline;
border: none;
margin: 0 0 -5px;
background: none;
}
.ui-tabs-anchor {
color: #000034;
}
ul.ui-widget-header, ul.ui-widget-content, ul.ui-state-default, ul.ui-state-hover {
background: none;
border: none;
}
.ui-tabs-active a {
text-decoration: none;
background: none;
color: #222222;
position: relative;
z-index: 5;
}
And some javascript to adjust the width of the tabs:
jQuery(document).ready(function($) {
var numTabs = $('#tabs li').length;
var tabWidth = 100 / numTabs;
var tabPercent = tabWidth + "%";
var liArray = $('#tabs li');
var i;
for (i = 0; i < liArray.length; i++) {
liArray[i].style.width = tabPercent;
}
});
And finally, some HTML:
<div id="tabs">
<ul class="ui-widget-header ui-corner-top">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>
<div id="tabs1">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div id="tabs2">Phasellus mattis tincidunt nibh. Cras orci urna, blandit id, pretium vel, aliquet ornare, felis. Maecenas scelerisque sem non nisl. Fusce sed lorem in enim dictum bibendum.</div>
<div id="tabs3">Nam dui erat, auctor a, dignissim quis, sollicitudin eu, felis. Pellentesque nisi urna, interdum eget, sagittis et, consequat vestibulum, lacus. Mauris porttitor ullamcorper augue.</div>
</div>
So yeah, with these snippets in mind - how do I use Javascript to dynamically apply a style to my tabs so that the width of them all combined = 100% of the pane?
Thanks!
Change the li to a block element so you can set its width.
jQuery(document).ready(function($) {
$("#tabs").tabs();
var numTabs = $('#tabs li').length;
var tabWidth = 98 / numTabs; // using 98 because sometimes using 100 will cause last element to get "bumped" down below the first element.
var tabPercent = tabWidth + "%";
$('#tabs li').each(function () {
$(this).css("width", tabPercent);
$(this).css("display", "inline-block");
});
});
http://jsfiddle.net/bwzhLzco/1/

Categories