Please note that if there are single quotes in the code enclosed in echo "" , they will need to be escaped, i.e. put a backslash (\") in front of each of them, without parentheses, of course.
In general, it turned out as it turned out. You will have to decide for yourself how to specifically attach this to your topic - when you have time, it’s even fun to “rack your brain.” Thank you.
Good luck to you! See you soon on the pages of the blog site
You might be interested
WebPoint PRO is a responsive WordPress theme with wide functionality and competent technical search engine optimization
Share42 - a script for adding social networking buttons and bookmarks to the site (there is a floating panel option)
When creating a website, there is often a need to keep in view the horizontal menu, which contains the main navigation of the site. The method of “fixing” the horizontal menu is convenient from the point of view of the visitor, who “has navigation always at hand”, no matter how much you turn the wheel down or up.
Fixing the horizontal menu with CSS: position:fixed On the one hand, everything is simple and easy to solve using CSS in no time. Example of HTML layout of a fixed horizontal menu:
- home
- News
- Contacts
- Search
[page content] [site footer]CSS layout of a fixed horizontal menu:
# menu-top-almost-fixed( position: fixed; top: 10px; left: 0; height: 30px; width: 100%; margin: 0; )
Now let’s set the indentation for the page content equal to the height of the menu:
#content ( margin-top: 30px; )
And now we have “almost” succeeded. The menu is always visible to the visitor. But what should we do if we have a site header in our design, followed by the menu itself, and in the header we have a logo, site motto, and banners.
Well, we can fix the site header by making the content indentation equal in height to the header and menu, along with the indentation between them. But a problem arises. We significantly limit the viewing space for our visitors to view page content. The option to abandon the cap does not suit us at all.
Fixing a horizontal menu using javascript So, let's consider the option when the menu “goes” behind the site header, but if the visitor actively scrolls down, the menu is “fixed” at the top and remains in place. The site header is not visible. If the visitor returns to the page header, the menu “becomes” in its place “behind the site header”. To begin with, here is the complete HTML layout of an example page layout:
Website logo Website slogan Banner
- home
- News
- Contacts
- Search
[page content] [site footer]Our website template consists of several typical areas:
- site headers – #header, height 150px
- horizontal menu – #menu-top-almost-fixed– height 30px,
- main information area of the page – #content,
- site footer - #footer.
Here's the CSS layout:
#menu-top-almost-fixed( position: fixed; margin: 0; left: 0; top: 150px; height: 30px; ) #header( display: block; height: 150px; overflow: hidden; position: relative; margin -bottom: 55px; ) #menu-top-almost-fixed ul, #menu-top-almost-fixed li( list-style: none; margin: 0; padding: 0; ) #menu-top-almost-fixed ul ( display: block; text-align: center; width: 100%; float: left; ) #menu-top-almost-fixed ul li( display: inline; line-height: 30px; width: 120px; padding: 0 5px ; text-align: center; )
First, let's set an indent from the header to the content equal to the height of our menu + a small indent with a margin for aesthetic beauty. #header ( margin-bottom: 55px; ) . Let's fix our menu right behind the header: #menu-top-almost-fixed( position: fixed; margin: 0; left: 0; top: 150px; height: 30px; ) .
Now let’s make sure that when scrolling, the menu “fixes” exactly at the top of the page. Let's put the following javascript between and :
Javascript:
var m1 = 150; /* header height in pixels */ var m2 = 2; /* indentation when the header is no longer visible during scrolling */ var menuID = "menu-top-almost-fixed"; /* id of the horizontal menu to pin */ var menuOpacityOnChange = "0.7"; /* menu transparency when scrolling: 1 - opaque, 0.5 - translucent 0.0 - completely transparent */ var menuOpacityOnChangeIE = menuOpacityOnChange * 100; /* function for cross-browser determination of the indent from the top of the document to the current position of the scroll scroller */ function getScrollTop() ( var scrOfY = 0; if(typeof(window.pageYOffset) == "number") ( //Netscape compliant scrOfY = window.pageYOffset ; ) else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) ( //DOM compliant scrOfY = document.body.scrollTop; ) else if(document.documentElement && (document.documentElement. scrollLeft || document.documentElement.scrollTop)) ( //IE6 Strict scrOfY = document.documentElement.scrollTop; ) return scrOfY; ) /* function that sets the top padding for a floating fixed horizontal menu depending on scroller position and visibility headers */ function marginMenuTop() ( var top = getScrollTop(); var s = document.getElementById(menuID); if(typeof s != "undefined" && s)( if (top+m2< m1) {
s.style.top = (m1-top) + "px";
s.style.filter = s.style.filter.replace("progid:DXImageTransform.Microsoft.Alpha(opacity="+menuOpacityOnChangeIE+")","");
s.style.opacity = "1";
} else {
s.style.top = m2 + "px";
s.style.opacity = menuOpacityOnChange;
s.style.filter = s.style.filter.replace("progid:DXImageTransform.Microsoft.Alpha(opacity="+menuOpacityOnChangeIE+")","");
s.style.filter += "progid:DXImageTransform.Microsoft.Alpha(opacity="+menuOpacityOnChangeIE+")";
}
}
}
/** функция регистрирует
вычисление позиции
«плавающего» меню при прокрутке страницы
**/
function setMenuPosition(){
if(typeof window.addEventListener != "undefined"){
window.addEventListener("scroll", marginMenuTop, false);
} else if(typeof window.attachEvent != "undefined"){
window. attachEvent("onscroll", marginMenuTop);
}
marginMenuTop();
}
/** регистрируем вызов
необходимых функций после
загрузки страницы **/
if(typeof window.addEventListener != "undefined"){
window.addEventListener("load", setMenuPosition, false);
} else if(typeof window.attachEvent != "undefined"){
window. attachEvent("onload", setMenuPosition);
}
You can see an example of the implementation by following this link and using the scroll wheel. So, everything is simple here. In the settings we pass the following parameters to the script:
- var m1 = 150; - header height in pixels,
- var m2 = 2; - indentation when the header is no longer visible while scrolling,
- var menuID = “menu-top-almost-fixed”; - id of the horizontal menu to pin,
- var menuOpacityOnChange = “0.7”; - menu transparency when scrolling:
- 1 - opaque
- 0.5 – translucent
- 0.0 - completely transparent
In this version, we have “tuned” our menu a little, and when scrolling we add translucency to it. A more classic option immediately suggests itself, when we do not change the transparency of the menu, but simply make a background for the menu in the form of a background with the menu color and a lower translucent border (in which the gradient smoothly “transitions” from an opaque color to a transparent one):
Let's change a little CSS layout for our horizontal fixed menu:
#menu-top-almost-fixed( position: fixed; margin: 0; left: 0; top: 150px; height: 30px; background: url(./images/white-gradient-l.png) bottom left repeat-x ; )
Now let’s give the modified javascript code, which we will place between and :
Javascript:
var m1 = 150; /* header height in pixels */ var m2 = 0; /* indentation when the header is no longer visible during scrolling */ var menuID = "menu-top-almost-fixed"; /* function for cross-browser determination of the indent from the top of the document to the current position of the scroll scroller */ function getScrollTop() ( var scrOfY = 0; if(typeof(window.pageYOffset) == "number") ( //Netscape compliant scrOfY = window.pageYOffset ; ) else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) ( //DOM compliant scrOfY = document.body.scrollTop; ) else if(document.documentElement && (document.documentElement. scrollLeft || document.documentElement.scrollTop)) ( //IE6 Strict scrOfY = document.documentElement.scrollTop; ) return scrOfY; ) /* function that sets the top padding for a floating fixed horizontal menu depending on scroller position and visibility headers */ function marginMenuTop() ( var top = getScrollTop(); var s = document.getElementById(menuID); if(typeof s != "undefined" && s)( if (top+m2< m1) {
s.style.top = (m1-top) + "px";
} else {
s.style.top = m2 + "px";
}
}
}
/** функция регистрирует
вычисление позиции
«плавающего» меню при прокрутке страницы
**/
function setMenuPosition(){
if(typeof window.addEventListener != "undefined"){
window.addEventListener("scroll", marginMenuTop, false);
} else if(typeof window.attachEvent != "undefined"){
window. attachEvent("onscroll", marginMenuTop);
}
}
/** регистрируем вызов
необходимых функций после
загрузки страницы **/
if(typeof window.addEventListener != "undefined"){
window.addEventListener("load", setMenuPosition, false);
} else if(typeof window.attachEvent != "undefined"){
window. attachEvent("onload", setMenuPosition);
}
So, everything is simple here. In the settings we pass the following parameters to the script:
- var m1 = 150; - header height in pixels,
- var m2 = 0; - indentation when the header is no longer visible during scrolling.
The menu works fine, but if you reload the page, the menu appears with the first indent If there is such a problem, you need to call the menu after loading the page one time. To do this, change the function call code from:
Function setMenuPosition())( if(typeof window.addEventListener != "undefined")( window.addEventListener("scroll", marginMenuTop, false); ) else if(typeof window.attachEvent != "undefined")( window. attachEvent( "onscroll", marginMenuTop); ) );
To the following code:
Function setMenuPosition())( if(typeof window.addEventListener != "undefined")( window.addEventListener("scroll", marginMenuTop, false); ) else if(typeof window.attachEvent != "undefined")( window. attachEvent( "onscroll", marginMenuTop); ) marginMenuTop(); );
After the page loads, we immediately call our marginMenuTop function, which will check the position of the menu on the page and apply the desired style
Implementing a partially fixed menu using the Afixx jQuery plugin from Twitter Bootstrap In continuation of this topic, an article was written for you on implementing an almost fixed menu using the jQuery Affix plugin from the Twitter Bootstrap framework.
Recently, a trend has become fashionable: a fixed menu when scrolling a page. Usually this is a horizontal menu on Landing Page sites.
How it works is that when the page loads, the menu is located in a certain place on the page (for example, under the “header”), and when you scroll the page, it is fixed at the top of the browser window and does not scroll like other content.
If the visitor scrolls up the page and reaches the beginning of the page, the menu returns to its place. Thus, the visitor, being anywhere on the page, can use it and go to other pages of the site. This is very convenient and complies with usability principles.
Now I will tell you how to implement such a menu by spending a minimum of your time and without resorting to the help of professionals.
First, we need to connect the jQuery library for the site
<
script src=
"//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"
>
You can connect locally or via Google.
To connect locally, you need to download the jQuery file from the official website http://jquery.com/
CSS
JavaScript
In the script, we create 2 variables in which we save the values of the height of the header and the indentation of the block with the menu at the top. There may be no indentation (as in this case). Then we write a handler for the onScroll event of the window object. In it, using the scrollTop() method, we calculate the distance from the top of the page to the current position of the scroll scroller. Based on the calculation, we position the block with the menu.
That's all, thanks to simple solutions you can achieve a beautiful fixed menu that will not scroll through when scrolling the main part of the site
The first thing we will do is paste our HTML code in the place on your site where you want to see the menu.
- home
- WordPress
- HTML5&CSS3
- PHP
The menu is assigned the default class, thanks to which our jquery can then determine that this particular block then needs to be pinned to the top.
2. jQuery code In the header, before the closing head, insert the code. As I wrote above, it defines a block with class default and after scrolling assigns it class fixed . You can change the class names if you need it. But just be careful and don’t miss anything, otherwise everything will simply stop working. You need to change in jQuery, HTML and CSS.
$(document).ready(function())( var $menu = $("#menu"); $(window).scroll(function())( if ($(this).scrollTop() > 100 && $menu. hasClass("default"))( $menu.fadeOut("fast",function())( $(this).removeClass("default") .addClass("fixed transbg") .fadeIn("fast"); )) ; ) else if($(this).scrollTop()