Moving to a specific section on an HTML Page - html

I have an a tag that I am using to specify the section on an HTML page where I want to move when a link is clicked:
<a name="ourServicesSection"></a>
This works fine, the only problem is that I have a fixed navbar on top of the page. So, when I click the link it moves to the section of the page, but a slice of it is covered up by the navbar. Is there a way I can "offset" the move by the size of the navbar? How would I do that?
Thanks

Putting a margin/padding at the top of whatever element you're scrolling to seems to be the common practice. Or move up the achor destination.

You can use Javascript (jQuery) to handle the scroll with offset or reposition element using margin

If you are willing to use jQuery, something like this would work.
$('a[href^="#"]').on('click', function(e) {
e.preventDefault();
var scrollTo = $(this).attr('href');
var pageOffset = $(scrollTo).offset().top - ($(window).height() / 2);
$(document).scrollTop(pageOffset);
});
Example: http://jsfiddle.net/vDQaT/

Related

How to make a go down button? So when clicking it will redirect me a dew pixels down

So I have this small icon and I want it to go down a little just enough to cover the entire part of 1, you click on it, and it moves you smoothly to 1. How do I code that?
give the next element to the arrow some id for example if it's a div, add this to it:
<div id="header">some content...</div>
the wrap the arrow inside an anchor tag and give it the id of the element you want to go to like this :
<a href="#header">
<p>Go down 🡣</p>
</a>
Vanilla JS implementation:
window.scrollTo(0, 50);
or
window.scrollTo({ top: 50, behavior: 'smooth' });
where 50 is amount of pixels. Link to MDN Window.scrollTo
Scroll to particular element:
var element = document.getElementById("box");
element.scrollIntoView();
Link to MDN ScrollIntoView

Back to Top Link with a slow animation (CSS only)

I have a simple
Back to Top
and this puts me to the top of the page immediately. But I want it to go to the top of the page slowly with just CSS.
Is this possible?
Thanks in Advance.
Edit:
Okay, for anyone with the same problem. It did it with this JQuery:
// #btt is ID of the Back to Top Link
$("#btt").click(function () {
//html and body is used because of Browser compatibilit
$("html, body").animate({scrollTop: 0}, 1000);
});
You can't activate actions like scrolling with pure CSS, sorry. You'll need some jQuery in there.
What you CAN do is trick the user in thinking the page has scrolled by moving elements around with CSS on :hover, but that's a really bad practice, and would require much more coding that a simple jQuery function.

Always visible div while scrolling

On my aspx page, I have two left and right portions. I want to show always left side (which is actually a 'div' containig treeview) while scrolling the right side (which are actual contents of page). Thanks
You need to put position: fixed; on the div element. That will anchor it to the viewport.
Hi I found the best solution! As always JQUERY saving my life !!
Just put a Div called as you wan, I've chosen the same in the example below: #scrollingDiv.
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script>
$().ready(function() {
var $scrollingDiv = $("#scrollingDiv");
$(window).scroll(function(){
$scrollingDiv
.stop()
.animate({"marginTop": ($(window).scrollTop() )}, "slow" );
});
});
</script>
I took that code from a website, it works and it's pretty easy to understand.
Fast forward to 2020, and it is now possible to do this with CSS.
<div style="position: sticky; top: 0;"> Tree view </div>
The user npas explains this quite nicely:
top is the distance to the viewport top the div should stay at when you scroll. Specifying top is mandatory. (…)
The sticky div will act like a normal div in all ways except when you scroll past it, then it will stick to the top of the browser.
Here's a jsfiddle to give you an idea.
MDN documentation
Supported by all modern browsers
You need to set the position of the div to Fixed in CSS. See this link for more information. You will need to set position using the top and left in css as well so it knows where to fix it!
The problem is that when the block moves, it gets attention and ability to concentrate on reading.
To remedy this using this function.
This code is perfect :
(change "220" and "46px" if necessary)
var $scrollingDiv = $("#scrollingDiv");
$(window).scroll(function(){
if ($(window).scrollTop()>220) {
$scrollingDiv
.css("position",'fixed' )
.css("top",'46px' )
} else {
$scrollingDiv
.css("position",'' )
.css("top",'' )
}
});

link to anchor near bottom of page

I'm doing some documentation where I make heavy use of anchors for linking between pages on a wiki.
see here:
http://code.google.com/p/xcmetadataservicestoolkit/wiki/ServicesExplained#Platform_Data_Structures
The feature that really makes this work well is when the browser shows the anchor at the absolute top of the pane. When it gets confusing is when linking to an anchor shows the anchor half-way down the page since the page is scrolled down all the way
see here:
http://code.google.com/p/xcmetadataservicestoolkit/source/browse/trunk/mst-common/src/java/xc/mst/utils/Util.java#227
My solution in the wiki (first link) was to put a blank image at the bottom of the page simply to make the browser show the anchor right at the top. Is there a better way to do this? Is there a way to do it in the second link (in which I can't add a blank image)?
Putting a blank image at the bottom of your page is a bad idea, since it will expand your document to a unnecessary height.
You could throw in some javascript to apply an effect to the anchor you just travelled to, to highlight it wherever it is.
Without altering the height of your document (i.e. adding extra padding at bottom), you'll always have this issue.
However, using bit of JS/jQuery, the user experience can be improved considerably:
On clicking a named anchor:
Instead of jumping in a flash (broswer's default behavior), add a smooth scroll
add an highlight to indicate current selection (this helps tremendously in 2nd case as the user can clearly see what is current)
Created a demo to illustrate the concepts: http://jsfiddle.net/mrchief/PYsyN/9/
CSS
<style>
.current { font-weight: bold; }
</style>
JS
function smoothScroll(elemId) {
// remove existing highlights
$('.current').css({backgroundColor: "transparent"}).removeClass('current');
var top = $(elemId).offset().top;
// do a smooth scroll
$('html, body').animate({scrollTop:top}, 500, function(){
// add an highlight
$(elemId).animate({backgroundColor: "#68BFEF" }, 500, function () {
// keep tab of current so that style can be reset later
$(elemId).addClass('current');
});
});
}
// when landing directly
if (document.location.hash) {
smoothScroll(document.location.hash);
}
$('a[href*="#"]').click(function() {
// utilizing the fact that named anchor has a corresponding id element
var elemId = $(this).attr('href');
smoothScroll(elemId);
});
You can create a absolutre positioned pseudo-element with a great height to targeted block using just the following CSS (for the second link in your post:
#nums td:target a::before {
content: '';
position: absolute;
height: 700px;
}
The height must be around the height of the viewport, so the best solution is to create these styles on the fly using js. But if you don't wan't to use js, just use height: 1000px or more — if you don't mind a gap at the bottom of course.
The best part: it's only CSS and there would be no gap when no anchors are targeted.
Edit: just a sneak peek into the future: if the vw/vh units would come to other browsers (now it's only in IE9), this could be awesomely done with just CSS using height: 100vh :)
You could use Javascript / jQuery to create a white div that has the necessary height needed to put your element at the top of the browser window, and you could even remove this upon scrolling away.
However I would highly recommend against doing so as this will expand your page where it isn't needed. It's a lot smarter to simply style the tag upon going there (through Javascript / jQuery) so it pops out to the viewer, for instance by setting the font-weight to bold or changing the background-color.
I would probably use a combination of jQuery and PHP for this:
PHP(somewhere right after your <body> element):
<?php
$anchor = explode('#', $_SERVER['REQUEST_URI']);
$anchor = $anchor[1];
echo '<div id="selected-anchor" anchor="'.$anchor.'"></div>';
?>
And then the jQuery:
<script type="text/javascript">
$(function(){
$('#selected-anchor').css('background-color', '[Whatever highlight color you want]');
});
</script>
Hope this helps.

Fixed header inside scrolling block

I'm trying to create a block which may or may not have a scrollbar, with a header that does not scroll. The trick is that the width of the header should be affected by the presence of a scrollbar.
I'm worried that this is one of those CSS use cases which should be trivial, but might, in fact, be impossible. Anyone willing to prove me wrong?
Here are a few pointers
http://davidchambersdesign.com/css-fixed-position-headers/
and there involve tables with fixed header and scrolling body
http://imar.spaanjaars.com/357/a-scrollable-table-with-a-fixed-header
http://anaturb.net/csstips/sheader.htm
You cannot do this with CSS alone. We must use javaScript. With jQuery you can do the following
var cw = $('#container').innerWidth(),
cs = $('#container').scrollTop();
$('#header').css({
'width': cw + "px"
});
$('#container').scroll(function() {
$('#header').css({
'top': $('#container').scrollTop(),
})
})
Check working example at http://jsfiddle.net/VswxL/2/
I haven't figured out how to do this with CSS alone. So, here's a solution which uses JavaScript (here, jQuery), but only runs when he content changes. If the size of your wrapper depends on the size of the window, you may also need to run it on resize. Here's the heart of it:
$.fn.fitTo = function(target){
var $el = $(this);
$(target).bind('refit', function(){
$el.width(this.clientWidth);
});
}
Call $header.fitTo($content) to bind the header to a custom refit event on the element with the content. Now, whenever the content changes such that a scroll bar may have appeared or disappeared, do…
$content.trigger('refit');
…and the width of the header is reset to the clientWidth of the element containing content. The header must be outside the scrolling element.
Working example