Nav links should active for a particular section while scrolling - html

I had a multi-page website in that there are four links on the navbar. Out of four links two links redirects to other new pages. There is a section of content related to those links that are there on my landing page. I would like to enable those also. Kindly help in this situation the code I have implemented till today
<link href='https://fonts.googleapis.com/css?family=Lato:100,400,700' rel='stylesheet' type='text/css'>
<nav class="navigation" id="mainNav">
<a class="navigation__link" href="#1">home</a>
<a class="navigation__link" href="#2">about</a>
<a class="navigation__link" href="test.html">test</a>
<a class="navigation__link" href="#test1.html">test1</a>
</nav>
<div class="page-section home" id="1">
<h1>Smooth scroll, fixed jump menu with active class</h1>
</div>
<div class="page-section about" id="2">
<h1>Section Two</h1>
</div>
<div class="page-section" id="3">
<h1>Section Three</h1>
</div>
<div class="page-section" id="4">
<h1>Section Four</h1>
</div>
<div class="page-section test" id="5">
<h1>Section Five</h1>
</div>
<div class="page-section test1" id="6">
<h1>Section Six and this section is test section</h1>
</div>
<div class="page-section" id="7">
<h1>Section Seven and this section is test1</h1>
</div>
* {
font-family: 'Lato', sans-serif;
font-weight: 300;
transition: all .1s ease;
}
html, body {
height: 100%;
}
h1 { font-size: 64px; }
.page-section {
height: 480px;
width: 50%;
margin-left: 35%;
margin-top: 5%;
padding: 3em;
background: linear-gradient(45deg, #43cea2 10%, #185a9d 90%);
color: white;
box-shadow: 0px 3px 10px 0px rgba(0,0,0,0.5);
}
.navigation {
position: fixed;
width: 30%;
margin-left: 2%;
background-color: #999;
color: #fff;
&__link {
display: block;
color: #ddd;
text-decoration: none;
padding: 1em;
font-weight: 400;
&:hover {
background-color: #aaa;
}
&.active {
color: white;
background-color: rgba(0,0,0,0.1);
}
}
}
$(document).ready(function() {
$('a[href*=#]').bind('click', function(e) {
e.preventDefault(); // prevent hard jump, the default behavior
var target = $(this).attr("href"); // Set the target as variable
// perform animated scrolling by getting top-position of target-element and set it as scroll target
$('html, body').stop().animate({
scrollTop: $(target).offset().top
}, 600, function() {
location.hash = target; //attach the hash (#jumptarget) to the pageurl
});
return false;
});
});
$(window).scroll(function() {
var scrollDistance = $(window).scrollTop();
// Show/hide menu on scroll
//if (scrollDistance >= 850) {
// $('nav').fadeIn("fast");
//} else {
// $('nav').fadeOut("fast");
//}
// Assign active class to nav links while scolling
$('.page-section').each(function(i) {
if ($(this).position().top <= scrollDistance) {
$('.navigation a.active').removeClass('active');
$('.navigation a').eq(i).addClass('active');
}
});
}).scroll()

For more custom assignments of when what should happen, there is a (very old, but still working) jQuery plugin. See:
Github: jquery.inview
Tutorial: Element 'in view' Event Plugin
Usage:
$('#mySection1').on('inview', function(event, isInView) {
if (isInView) {
$('#myNavOption1').addClass('active');
} else {
$('#myNavOption1').removeClass('active');
}
});

What you are looking for is a scrollspy:
Scrollspy · Bootstrap
or if that is not suitable for you just google "scrollspy" and find other frameworks that may fit you more.

Related

Open a "modal" window

How can i make when I click on the card it opens a "modal window" with information about the product.
<div class="card">
<div class="imgBox">
<img src="./img/bau.png" alt="Produto" class="mouse">
</div>
<div class="contentBox">
<h3>Plugin</h3>
<h2 class="price">25.<small>00</small> BRL</h2>
Comprar Agora!
</div>
</div>
There are several approaches. And there is no real right or wrong here either.
The approach has to fit your application. If you always try to keep the approach somewhat abstract, there is nothing fundamentally wrong with it.
In the example below, I have taken the linked modal example from the comment
below your question and adapted the following.
added a data object in which I manage the corresponding contents of the modal.Here you can also use an API call against an interface.
I have assigned an EventListener to all buttons.
The parts that are variable in the modal are exchanged with the corresponding content when clicked.
Done!
const modalData = [
{id: 1, title: "Title One", content: "bla"},
{id: 2, title: "Title Two", content: "bla blu"},
];
// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btns = document.querySelectorAll(".myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btns.forEach(b => {
b.addEventListener('click', (e) => {
modal.style.display = "block";
const dataId = e.target.getAttribute("data-id")
const data = modalData.filter(m => m.id == dataId);
const modalTitle = document.querySelector("#myModal .title");
const modalContent = document.querySelector("#myModal .content");
modalTitle.innerHTML = data[0].title;
modalContent.innerHTML = data[0].content;
})
});
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
body {font-family: Arial, Helvetica, sans-serif;}
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
-webkit-animation-name: animatetop;
-webkit-animation-duration: 0.4s;
animation-name: animatetop;
animation-duration: 0.4s
}
/* Add Animation */
#-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
#keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/* The Close Button */
.close {
color: white;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
.modal-body {padding: 2px 16px;}
.modal-footer {
padding: 2px 16px;
background-color: #5cb85c;
color: white;
}
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h2>Animated Modal with Header and Footer</h2>
<!-- Trigger/Open The Modal -->
<button class="myBtn" data-id="1">Open Modal 1</button>
<button class="myBtn" data-id="2">Open Modal 2</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<h2 class="title">Modal Header</h2>
</div>
<div class="modal-body content">
</div>
<div class="modal-footer">
<h3>Modal Footer</h3>
</div>
</div>
</div>
</body>

Change background-image when hovering over a link?

I am currently trying to change the entire page background when I hover over two different links, rather than the background of the link which is occurring now. Any help on this would be greatly appreciated!
home.component.html
<div class="home">
<app-header></app-header>
<div class="locations">
<p>4698 5th Ave - New York, NY </p>
<a routerLink="/gotham" routerLinkActive="active">GOTHAM</a>
<a routerLink="/zion" routerLinkActive="active">ZION</a>
</div>
</div>
home.component.css
a {
color: white;
text-decoration: none;
display: inline;
font-size: 4vw;
margin: 0;
font-weight: normal;
padding: 30px;
}
a:hover {
text-decoration: underline;
background-image: url('https://images.unsplash.com/photo-1531973819741-e27a5ae2cc7b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1740&q=80');;
}
is that you want something like this
a {
color: white;
text-decoration: none;
display: inline;
font-size: 4vw;
margin: 0;
font-weight: normal;
padding: 30px;
}
.locations:hover {
text-decoration: underline;
background-image: url('https://images.unsplash.com/photo-1531973819741-e27a5ae2cc7b?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1740&q=80');;
}
<div class="home">
<app-header></app-header>
<div class="locations">
<p>4698 5th Ave - New York, NY </p>
<a routerLink="/gotham" routerLinkActive="active">GOTHAM</a>
<a routerLink="/zion" routerLinkActive="active">ZION</a>
</div>
</div>
You can use this syntax if they are siblings.
a:hover + .siblingClass {
property: value;
}
IMO you'd need plain JS for this. First assign Ids to each of your links. Then set up listeners for mouseover and mouseout events.
https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseover_event
https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseout_event
document.getElementById("link1").onmouseover = (e) => document.body.style.backgroundImage = "url(https://your.image.url.for.link1)"
document.getElementById("link2").onmouseover = (e) => document.body.style.backgroundImage = "url(https://your.image.url.for.link2)"
document.getElementById("link1").onmouseout = (e) => document.body.style.backgroundImage = ""
document.getElementById("link2").onmouseout = (e) => document.body.style.backgroundImage = ""

How do you display a particular element on hover?

I'm trying to make a navigation bar that displays a particular element when hovered and clicked. I'd also like the other elements to remain hidden. I tried doing a few codes but it won't work even if I hovered it already.
.yearly {
display: none;
}
.evento {
display: none;
}
.year a:hover+.yearly {
display: inline-block;
background-color: #aacde2;
}
.event a:hover+.evento {
display: inline-block;
background-color: #aacde2;
font-size: 50px;
}
<div class="listo">
<ul>
<li class="year">Yearly Donations</li>
<li class="event">Events</li>
</ul>
</div>
<div class="content">
<div class="yearly">
<img src="../images/pic1.jpg" alt="Photo" width="300px">
</div>
<div class="evento">
<p>trial</p>
</div>
</div>
You can achieve this using Jquery.
$(".year a").on({
mouseover: function(e){
$(".yearly").show();
},
mouseout: function(e) {
$(".yearly").hide();
}
});
$(".event a").on({
mouseover: function(e){
$(".evento").show();
},
mouseout: function(e) {
$(".evento").hide();
}
});
.yearly {
display: none;
}
.evento {
display: none;
}
.year a:hover + .yearly {
display: inline-block;
background-color: #aacde2;
}
.event a:hover + .evento {
display: inline-block;
background-color: #aacde2;
font-size: 50px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="listo">
<ul>
<li class="year">Yearly Donations</li>
<li class="event">Events</li>
</ul></div>
<div class="content">
<div class="yearly">
<img src="../images/pic1.jpg" alt="Photo" width="300px">
</div>
<div class="evento">
<p>trial</p>
</div>
</div>
You could call a Javascript function on hover. I'm sure there are better ways, but this works for example:
function displayYearly() {
document.getElementById("yearly").style.display = "inline-block";
}
function hideYearly() {
document.getElementById("yearly").style.display = "none";;
}
function displayEvento() {
document.getElementById("evento").style.display = "inline-block";
}
function hideEvento() {
document.getElementById("evento").style.display = "none";
}
#yearly {
display: none;
}
#evento {
display: none;
}
<div class="listo">
<ul>
<li class="year" onmouseover="displayYearly()" onmouseout="hideYearly()">
Yearly Donations
</li>
<li class="event" onmouseover="displayEvento()" onmouseout="hideEvento()">
Events
</li>
</ul>
</div>
<div class="content">
<div id="yearly">
<img src="https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon#2.png" alt="Photo" width="100px">
</div>
<div id="evento">
<p>trial</p>
</div>
</div>

How to make an image a 'jump to' button?

I have a small icon image of 'Arrow Up' at the bottom of my page.
How do I make this image a clickable button that jumps to the top of the page?
I am new to HTML and this is my first project so please bear with me.
So far I have tried:
input type="button" id="btnx" style="background-image:url('arrowup.png')"
Try this: Wrap your image in an anchor (A tag) link "#" will go to top
<img src="/some-image-folder/arrowup.png">
maybe your html is like this:
<i class="fa fa-arrow-up" aria-hidden="true"></i>
maybe you'd like to use the input button? Ok:
<form>
<input type="button" value="Click me" onclick="$(window).scrollTop(0);">
</form>
If you dont want to just jump to the top here is an animated method.
Animated with Image:
function topFunction() {
if (document.body.scrollTop !== 0 || document.documentElement.scrollTop !== 0) {
window.scrollBy(0, -50);
requestAnimationFrame(topFunction);
}
}
.test {
background-color: lightgrey;
padding: 30px;
height: 2500px;
margin-bottom: 10px;
}
<div class="test">Scroll to Bottom</div>
<img alt="Click" src="/images/arrowup.png">
Animated with Symbol:
function topFunction() {
if (document.body.scrollTop !== 0 || document.documentElement.scrollTop !== 0) {
window.scrollBy(0, -50);
requestAnimationFrame(topFunction);
}
}
.test {
background: lightgrey;
padding: 30px;
height: 2500px;
margin-bottom: 10px;
}
.uparrow {
color: white;
background: black;
font-size: 22px;
padding: 5px;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div class="test">Scroll to Bottom</div>
<i class="material-icons uparrow"></i>
Check this FIDDLE
HTML
<h1>Top of the page</h1>
<article style="height: 1000px">
<p style="margin-bottom: 600px">Scroll down the page…</p>
<p>Then click the box.</p>
Scroll
</article>
Script
$(document).ready(function () {
$(window).scroll(function () {
if ($(this).scrollTop() > 100) {
$('.scrollup').fadeIn();
} else {
$('.scrollup').fadeOut();
}
});
$('.scrollup').click(function () {
$("html, body").animate({
scrollTop: 0
}, 600);
return false;
});

How best to make a smileys box in html

I'd like to add a box containing smileys icons above the comment area which opens using jQuery on click. What I come up with is this:
<div class="emo">
<i href="#" id="showhide_emobox"> </i>
<div id="emobox">
<input class="emoticon" id="icon-smile" type="button" value=":)" />
<input class="emoticon" id="icon-sad" type="button" value=":(" />
<input class="emoticon" id="icon-widesmile" type="button" value=":D" /> <br>
</div>
</div>
css:
.emoticon-smile{
background: url('../smileys/smile.png');
}
#icon-smile {
border: none;
background: url('../images/smile.gif') no-repeat;
}
jQuery:
// =======show hide emoticon div============
$('#showhide_emobox').click(function(){
$('#emobox').toggle();
$(this).toggleClass('active');
});
// ============add emoticons============
$('.emoticon').click(function() {
var textarea_val = jQuery.trim($('.user-comment').val());
var emotion_val = $(this).attr('value');
if (textarea_val =='') {
var sp = '';
} else {
var sp = ' ';
}
$('.user-comment').focus().val(textarea_val + sp + emotion_val + sp);
});
However I have difficulty placing buttons in a nice array and make background image for them (the button values appear before image and the array is not perfectly rectangular. So I'm wondering maybe this is not the best way to render this box.
Any ideas to do this properly?
First show images, on hover hide image and show text. No need for input elements to get text of Dom Node
Something like this:
$(document).ready(function() {
$(".wrapper").click(function() {
var value = $(this).find(".smily-text").text();
console.log(value);
alert("Smily text is '" + value + "'");
});
});
.smily {
background: url(http://www.smiley-lol.com/smiley/manger/grignoter/vil-chewingum.gif) no-repeat center center;
width: 45px;
height: 45px;
}
.smily-text {
display: none;
font-size: 20px;
text-align: center;
line-height: 45px;
height: 45px;
width: 45px;
}
.wrapper {
border: 1px solid red;
float: left;
cursor: pointer;
}
.wrapper:hover .smily {
display: none;
}
.wrapper:hover .smily-text {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<div class="smily"></div>
<div class="smily-text">:)</div>
</div>
<div class="wrapper">
<div class="smily"></div>
<div class="smily-text">:(</div>
</div>
<div class="wrapper">
<div class="smily"></div>
<div class="smily-text">:]</div>
</div>
<div class="wrapper">
<div class="smily"></div>
<div class="smily-text">:[</div>
</div>