Hello everyone,
I got a small piece of code, which doesn’t really do anything special with jquery, but I still wrote it using jquery library.
Now I want to get rid of the library from 2 of my pages which uses the excact same code and convert my to normal JS.
Here:s a snippet:
var show = false;
$(document).ready(function() {
validateCSS();
});
$(window).resize(function() {
if ($(window).width() > 600)
switchContent(2);
validateCSS();
});
$(window).scroll(function() {
validateCSS();
});
function validateCSS() {
if ($(window).scrollTop() > $('header').eq(0).height() + ($(window).width() < 1500 ? 42 : 0))
$('#search_form').css('top', '0');
else
$('#search_form').css('top', $('header').eq(0).height() - $(window).scrollTop() + ($(window).width() < 1500 ? 42 : 0) + 'px');
}
function switchContent(num) {
if (num == 2)
{
show = false;
$('#search_icon').find('img').attr('src', '/images/search_icon.jpg');
$('#search_form').removeAttr('style');
$('#gallery_holder').removeAttr('style');
$('#content_wrap').prepend($('#search_form'));
}
else
{
if (show)
{
show = false;
$('#search_icon').find('img').attr('src', '/images/search_icon.jpg');
$('#gallery_holder').css('display', 'block');
$('#search_form').css('display', 'none');
$('#content_wrap').prepend($('#search_form'));
}
else
{
show = true;
$('#search_icon').find('img').attr('src', '/images/search_icon2.jpg');
$('#content_wrap_inner').find('h2').after($('#search_form'));
$('#gallery_holder').css('display', 'none');
$('#search_form').css('display', 'block');
}
}
}
Here is what I did so far, but doesn’t seem to have support for all browsers…
var doc = document.documentElement,
header = document.getElementsByTagName('header')[0],
form = document.getElementById('search_form'),
img = document.getElementById('search_icon').getElementsByTagName('img')[0],
gallery = document.getElementById('gallery_holder'),
wrap = document.getElementById('content_wrap'),
inner = document.getElementById('content_wrap_inner'),
h2 = document.getElementsByTagName('h2')[0],
show = false;
window.onload = function()
{
validateCSS();
}
window.onscroll = function()
{
validateCSS();
}
window.onresize = function()
{
if (window.innerWidth > 600)
switchContent(2);
validateCSS();
}
function validateCSS()
{
if ((window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0) > header.offsetHeight + (window.innerWidth < 1500 ? 42 : 0))
form.style.top = '0';
else
form.style.top = header.offsetHeight - (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0) + (window.innerWidth < 1500 ? 42 : 0) + 'px';
}
function switchContent(num)
{
if (num == 2)
{
show = false;
img.src = '/images/search_icon.jpg';
form.removeAttribute('style');
gallery.removeAttribute('style');
wrap.insertBefore(form, inner);
}
else
{
if (show)
{
show = false;
img.src = '/images/search_icon.jpg';
form.style.display = 'none';
gallery.style.display = 'block';
wrap.insertBefore(form, inner);
}
else
{
show = true;
img.src = '/images/search_icon2.jpg';
h2.parentNode.insertBefore(form, h2.nextSibling);
gallery.style.display = 'none';
form.style.display = 'block';
}
}
}