I'm new to jQuery and I managed to figure out most of what I wanted it to do, but I've been having problems.
When the user scrolls down, the menu bar goes with them. However, the search form appears multiple times. If the user scrolls up and down multiple times, the many search forms will fill up the entire page.
This is what my script looks like now:
<script>
$(document).ready(function () {
var menu = $('.yamm');
var origOffsetY = menu.offset().top;
function scroll() {
if ($(window).scrollTop() >= origOffsetY) {
$('.yamm').addClass('sticky');
$('.content').addClass('menu-padding');
$('<form class="form-inline"><input type="text" class="form-control" placeholder="Search over 80,000 products" style="width: 80%;"><button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span></button></form>').appendTo('.searchmenu');
} else {
$('.yamm').removeClass('sticky');
$('.content').removeClass('menu-padding');
$('.searchmenu .form-inline').remove();
}
}
document.onscroll = scroll;
});</script>
What am I missing to make the search form appear only once?
Thank you for your help.
I recommend to show/hide the form instead of creating a new one each time the user scrolls down from the top. With this approach you keep all your html in the same file.
Html file:
Script:
You can also make the form fadeIn/fadeOut, instead of show/hide.