Changing the appearance of navigation buttons in response to scrolling - html

At the top of a page I'm building,
There will be some navigation buttons for navigating internally within the page.
When hovered over, a button will change its appearance (for instance, the font color and the color of the button itself will switch places).
How do I make the buttons change their appearance in the same way once the user scrolls down to the part of the page the button links to? For instance, there is a button labeled "Contact" that links links to the "Contact" section of the page. How should one change the button's font color from (for instance) white to blue and the background color from blue to white when the user scrolls down to that part of the page?
This was already discussed elsewhere, but I'm not yet familiar with headroom.js.

We could use a simple onclick color change behavior with jquery since everything else is already working fine. So what you could do is just change the color on click and remove it from the previous one simontaneously.
For the following HTML:
<nav>
<li id="1"><a href="#" >One</a></li>
<li id="2"><a href="#" >Two</a></li>
<li id="3"><a href="#" >Three</a></li>
<li id="4"><a href="#" >Four</a></li>
</nav>
If onclick color remains same for every nav item then
jQuery(document).ready(function($) {
$("nav li").each(function() {
$(this).click(function() {
$(this).prev().find("a").css("color", "red");
$(this).next().find("a").css("color", "red");
$(this).find("a").css("color", "blue");
});
});
});
If each color is differrent.
jQuery(document).ready(function($) {
$("#1").click(function() {
$(this).prev().find("a").css("color", "red");
$(this).next().find("a").css("color", "red");
$(this).find("a").css("color", "blue");
});
})
Instead of .css you can also add a class like
jQuery(document).ready(function($) {
$("nav li").each(function() {
$(this).click(function() {
$(this).prev().find("a").removeClass("active");
$(this).next().find("a").removeClass("active");
$(this).find("a").addClass( "active" );
});
});
});
https://jsfiddle.net/yuhazt0p/1/

Related

Get the active link to remain bold and not only become bold when I click on it

I am aware of both :visited and :active css classes, but both does not answer my need!
I want to remain the active link styled with Red Background Color (or whatever design) as long as the related page of the active link is active for the user.
Below is my code:
HTML:
<a class="link1" href="/page-1">Link 1</a>
<a class="link2" href="/page-2">Link 2</a>
<a class="link3" href="/page-3">Link 3</a>
<a class="link4" href="/page-4">Link 4</a>
CSS:
link2:visited {
background-color: red;
}
Or
link2:active {
background-color: red;
}
The :active class will take effect when the user click down the mouse button and end when the user will release up the mouse button.
The :visited class will remain the link with red background even after the user will click on another menu link.
so both :visited and :active css classes does answer my question !!!
The below images will explain more:
Link 2 is active
Link 4 is active
As you can see in the above images when another link on the menu is clicked/visited, the previous link design will be reverted back to its previous state and only the current clicked/visited link will have a red background color as long as its correspondent page is active.
we can use jquery to add class to each link when it is an active state. then we can style the link with the basis of that class.
checkout this link for reference: https://www.w3schools.com/jquery/html_addclass.asp
thank you.
If you can use javascript,
On each page, at the bottom, add:
<script>
var ele = document.getElementsByClassName("link1");
ele[0].style.backgroundColor = "red";
<script>
change the class name for each page: link2 on page 2; link3 on page 3, etc.
I ended up using jQuery and JavaScript as below:
(function ($, window, Drupal) {
$(window).ready(function () {
// Get the current page url.
var pathname = window.location.pathname;
if (pathname == "/path1") {
$('.link1').addClass("active-page");
}
if (pathname == "/path2") {
$('.link2').addClass("active-page");
}
if (pathname == "/path3") {
$('.link3').addClass("active-page");
}
if (pathname == "/path4" || pathname == "/") {
$('.link4').addClass("active-page");
}
});
})(jQuery, window, Drupal);
The above method is working as expected at least for me!
I still needs an advise if this could affect the site performance in a way or another ?

Need to hide a tooltip after a few seconds from tap on mobile

Here's an anchor tag with a tooltip:
<a data-toggle="tooltip" data-original-title="Sorry, pronunciation audio not available!">
<span class="glyphicon glyphicon-volume-off pronounce">
</span>
</a>
On desktop screens, the tooltip only appears when the anchor is hovered over or clicked and vanishes when the mouse pointer moves away from it. However, on mobile screens, the tooltip does appear when the anchor is tapped but then stays on forever. Is there any way to make it fade out after a set period only on mobile devices?
UPDATE: For what it's worth, my tooltip JS looks like this:
$(document).ready(function() {
$("body").tooltip({ selector: '[data-toggle=tooltip]' });
});
This may help you :
$('body').click(function(event){
if (event.target.id != 'tooltip'){
setTimeout(function(){
$('#tooltip').tooltip('hide');
}, 2000);
}
});
JS FIDDLE : https://jsfiddle.net/b2t27sg0/3/

How to stop buttons from staying depressed with Bootstrap 3

How do you make a button in Bootstrap 3 undepress automatically after being clicked?
To replicate my problem, make a page with some buttons, give them appropriate bootstrap classes (and include bootstrap):
<input id="one" type="button" class="btn btn-default" value="one">
<input id="two" type="button" class="btn btn-primary" value="two">
Load the page and click on one of the buttons. It becomes depressed and highlighted until you click somewhere else on the page (using FF29 and chrome35beta).
Inspecting the input element while clicked and unclicked doesn't show any additional classes being attached and removed from it.
Here's an example of Bootstrap buttons staying depressed: http://jsfiddle.net/52VtD/5166/
In your example, the buttons do not stay depressed. They stay focused. If you want to see the difference, do the following:
Click and hold on a button.
Release. You will see that when you release the mouse the button's appearance changes slightly, because it is no longer pressed.
If you do not want your buttons to stay focused after being released you can instruct the browser to take the focus out of them whenever you release the mouse.
Example
This example uses jQuery but you can achieve the same effect with vanilla JavaScript.
$(".btn").mouseup(function(){
$(this).blur();
})
Fiddle
Or you can just use an anchor tag which can be styled exactly the same, but since it's not a form element it doesn't retain focus:
one.
See the Anchor element section here: http://getbootstrap.com/css/#buttons
The button remains focused. To remove this efficiently you can add this query code to your project.
$(document).ready(function () {
$(".btn").click(function(event) {
// Removes focus of the button.
$(this).blur();
});
});
This also works for anchor links
$(document).ready(function () {
$(".navbar-nav li a").click(function(event) {
// Removes focus of the anchor link.
$(this).blur();
});
});
My preference:
<button onmousedown="event.preventDefault()" class="btn">Calculate</button>
Or angular way:
function blurElemDirective() {
return {
restrict: 'E',
link: function (scope, element, attrs) {
element.bind('click', function () {
element.blur();
});
}
};
}
app.directive('button', blurElemDirective);
app.directive('_MORE_ELEMENT_', blurElemDirective);
Replace _MORE_ELEMENT_ with your others elements.
Or attribute way:
function blurElemDirective() {
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.bind('click', function () {
element.blur();
});
}
};
}
app.directive('blurMe', blurElemDirective);
Then add the attribute to your html element: blur-me
<button blur-me></button>
It's the browser's focus since it's a form element (input).
You can easily remove the focusing with a little css
input:focus {
outline: 0;
}
Here's the fiddle with your example: http://jsfiddle.net/52VtD/5167/
EDIT
Ah, I just saw now that the colour of the button itself changes too. Bootstrap changes the button of e.g. your btn-default button with this css:
.btn-default:focus {
color: #333;
background-color: #ebebeb;
border-color: #adadad;
}
If you don't want this behaviour, just overwrite it with your css.
This has to do with the :active and :focus element states. You need to modify the styles for those states for these buttons. For example, for the default button:
.btn-default:focus, .btn-default:active, .btn-default.active, .open .dropdown-toggle.btn-default {
color: #333;
background-color: #ccc;
border-color: #fff;
}

Is there a way to link my menu buttons so I can scroll to diffrent parts on the same webpage?

I have a webpage with a set of menubuttons, my question what's the easiest way
to scroll down my page by clicking a button, for example button middle will scroll
to the middle of the webpage, button bottom scrolls down to the bottom simply by using html/css.
In my case I have a button called gallery if I click on it I want my page to move
to the image gallery section on the same page.
<a href='#random'>Go</a>
<div id='random'>Hello</div>
Now when you click on the 'a' tag it will drop down to the div of id='random'
You could also use the following jQuery snippet to animate the scroll action:
$('a[href^="#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 900, 'swing', function () {
window.location.hash = target;
});
});
});
It takes ID as selector.

Jquery Hover show/hide issue

Im trying to setup so that when you hover over class .object1 -> in turn should reveal .obj_1 when you are not hovered on it, it should hide .obj_1. I may be a little off in my code, thanks for the help!.
$(document).ready(function() {
$(".obj_1 , .obj_2").hide();
});
$(".object1").hover(
function() { $(".obj_1").show(); },
function() { $(".obj_2").hide(); }
);
$(".object2").hover(
function() { $(".obj_2").show(); },
function() { $(".obj_1").hide(); }
);
Very simple it should be
$(document).ready(function() {
$(".obj_1 , .obj_2").hide();
});
$(".object1").hover(
function() { $(".obj_1").show(); },
function() { $(".obj_1").hide(); }
);
$(".object2").hover(
function() { $(".obj_2").show(); },
function() { $(".obj_2").hide(); }
);
The "hover" handler function signature is ( mouseInHandler, mouseOutHandler).
For object1 you want to show obj_1 on mouseIn, and hide it on mouseOut.
You don't need to reference obj_2 on object1 hover handlers.
Check out the fiddle I made here
FYI - the hover events act weird when you have complex inner content. ( for example, div within another div and so on ). I advise you to use "mouseenter" and "mouseleave"
UPDATING ANSWER AFTER REALIZING THIS IS A DROP DOWN MENU QUESTION
The drop down menu in CSS is a great example where "hover" won't suffice --> because the submenu disappears once you're not on the link anymore.. and that's not what we want.
It is important to note 3 things about drop down menus :
They can (?should?) be achieved purely with CSS
The HTML structure is important.
For example, consider the following structure instead :
<ul class="menu">
<li>
</li>
<li>
<ul class="menu">
<li>
</li>
</ul>
</li>
</ul>
This structure is recursive - you can have infinite levels of submenus - and the mouseenter/mouseleave on the "li" will hold since the submenu is part of the "li" item.
To see this in action have a look in my fiddle
Please also note that I removed the first "hide" from the onload code, and replaced it with css "display:none" - which resolves flickering on page load ( flickering means - first the submenu shows, and once the page loads, we hide it. )
A css solution would include a selector with "hover" on it ( yes, hover.. )
You can find plenty of blog posts about it while searching in google.
Here is the first one I found.