Implementare Stiky menu

Aici postaţi orice este legat de tehnologiile "client side". HTML, HTML5, XHTML, CSS, CSS3, XML, Standarde
Manual JS: http://www.phpromania.net/jsmanual/index.html

Moderatori: eyecon, Moderatori

indexcafea
Average Member
Mesaje: 65
Membru din: Mie Noi 07, 2012 9:18 pm
Contact:

Implementare Stiky menu

Mesajde indexcafea » Mar Apr 02, 2013 5:39 pm

Salut,

Vreau sa implementez un meniu javascript (stikymenu):

Am urmatorul cod:

Cod: Selectaţi tot

<script>
$(function() {

   // grab the initial top offset of the navigation
   var sticky_navigation_offset_top = $('#sticky_navigation').offset().top;
   
   // our function that decides weather the navigation bar should have "fixed" css position or not.
   var sticky_navigation = function(){
      var scroll_top = $(window).scrollTop(); // our current vertical position from the top
      
      // if we've scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative
      if (scroll_top > sticky_navigation_offset_top) {
         $('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 });
      } else {
         $('#sticky_navigation').css({ 'position': 'relative' });
      }   
   };
   
   // run our function on load
   sticky_navigation();
   
   // and run it again every time you scroll
   $(window).scroll(function() {
       sticky_navigation();
   });
   
   // NOT required:
   // for this demo disable all links that point to "#"
   $('a[href="#"]').click(function(event){
      event.preventDefault();
   });
   
});
</script>


Iar rezultatul se poate veadea la adresa www. mvc.anuntul10.ro

Iar in css am urmatorul id: #sticky_navigation { width:100%; height:50px; background: url(../images/trans-black-60.png); -moz-box-shadow: 0 0 5px #999; -webkit-box-shadow: 0 0 5px #999; box-shadow: 0 0 5px #999; }

As vrea ca atunci cand se da scroll si meniul devine fixed sa aiba alta forma, as putea sa creez un alt id cu alta formatare insa nu stiu cum sa modific javascriptul.

Multumesc anticipat!



Avatar utilizator
Andrei HAINAROSIE
PHPRomania Supporter
Mesaje: 15
Membru din: Joi Oct 18, 2012 8:20 pm
Localitate: Bucuresti
Contact:

Re: Implementare Stiky menu

Mesajde Andrei HAINAROSIE » Mie Apr 03, 2013 12:22 pm

Ai putea sa mai adaugi o clasa pe div#sticky_navigation.

Cod: Selectaţi tot

<script>
$(function() {

   // grab the initial top offset of the navigation
   var sticky_navigation_offset_top = $('#sticky_navigation').offset().top;
   
   // our function that decides weather the navigation bar should have "fixed" css position or not.
   var sticky_navigation = function(){
      var scroll_top = $(window).scrollTop(); // our current vertical position from the top
      
      // if we've scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative
      if (scroll_top > sticky_navigation_offset_top) {
         $('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 });
if (!$('#sticky_navigation').hasClass('fixed-menu')) {
  $(this).addClass('fixed-menu');
}
      } else {
if ($('#sticky_navigation').hasClass('fixed-menu')) {
  $(this).removeClass('fixed-menu');
}
         $('#sticky_navigation').css({ 'position': 'relative' });
      }   
   };
   
   // run our function on load
   sticky_navigation();
   
   // and run it again every time you scroll
   $(window).scroll(function() {
       sticky_navigation();
   });
   
   // NOT required:
   // for this demo disable all links that point to "#"
   $('a[href="#"]').click(function(event){
      event.preventDefault();
   });
   
});
</script>


Ai putea schimba apoi stilizarea utilizand clasa "fixed-menu".

indexcafea
Average Member
Mesaje: 65
Membru din: Mie Noi 07, 2012 9:18 pm
Contact:

Re: Implementare Stiky menu

Mesajde indexcafea » Mie Apr 03, 2013 12:49 pm

Andrei HAINAROSIE scrie:Ai putea sa mai adaugi o clasa pe div#sticky_navigation.

Cod: Selectaţi tot

<script>
$(function() {

   // grab the initial top offset of the navigation
   var sticky_navigation_offset_top = $('#sticky_navigation').offset().top;
   
   // our function that decides weather the navigation bar should have "fixed" css position or not.
   var sticky_navigation = function(){
      var scroll_top = $(window).scrollTop(); // our current vertical position from the top
      
      // if we've scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative
      if (scroll_top > sticky_navigation_offset_top) {
         $('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 });
if (!$('#sticky_navigation').hasClass('fixed-menu')) {
  $(this).addClass('fixed-menu');
}
      } else {
if ($('#sticky_navigation').hasClass('fixed-menu')) {
  $(this).removeClass('fixed-menu');
}
         $('#sticky_navigation').css({ 'position': 'relative' });
      }   
   };
   
   // run our function on load
   sticky_navigation();
   
   // and run it again every time you scroll
   $(window).scroll(function() {
       sticky_navigation();
   });
   
   // NOT required:
   // for this demo disable all links that point to "#"
   $('a[href="#"]').click(function(event){
      event.preventDefault();
   });
   
});
</script>


Ai putea schimba apoi stilizarea utilizand clasa "fixed-menu".


Ti-as fi recunoscator daca mi-ai da un raspuns mai explicit, ca la incepator

Avatar utilizator
Andrei HAINAROSIE
PHPRomania Supporter
Mesaje: 15
Membru din: Joi Oct 18, 2012 8:20 pm
Localitate: Bucuresti
Contact:

Re: Implementare Stiky menu

Mesajde Andrei HAINAROSIE » Mie Apr 03, 2013 1:19 pm

Cod: Selectaţi tot

<script>
$(function() {

   // grab the initial top offset of the navigation
   var sticky_navigation_offset_top = $('#sticky_navigation').offset().top;
   
   // our function that decides weather the navigation bar should have "fixed" css position or not.
   var sticky_navigation = function(){
      var scroll_top = $(window).scrollTop(); // our current vertical position from the top
     
      // if we've scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative
      if (scroll_top > sticky_navigation_offset_top) {
         $('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 });
if (!$('#sticky_navigation').hasClass('fixed-menu')) {
  $('#sticky_navigation').addClass('fixed-menu');
}
      } else {
if ($('#sticky_navigation').hasClass('fixed-menu')) {
  $('#sticky_navigation').removeClass('fixed-menu');
}
         $('#sticky_navigation').css({ 'position': 'relative' });
      }   
   };
   
   // run our function on load
   sticky_navigation();
   
   // and run it again every time you scroll
   $(window).scroll(function() {
       sticky_navigation();
   });
   
   // NOT required:
   // for this demo disable all links that point to "#"
   $('a[href="#"]').click(function(event){
      event.preventDefault();
   });
   
});
</script>


Daca schimbi js-ul tau cu acesta, o sa vezi ca div-ul cu id-ul "sticky_navigation" va mai primi o clasa "fixed-menu" atunci cand dai scroll in josul paginii.
Clasa respectiva va fi numai cand dai scroll in jos; te poti folosi de ea in fisierul tau de css (demo.css).

indexcafea
Average Member
Mesaje: 65
Membru din: Mie Noi 07, 2012 9:18 pm
Contact:

Re: Implementare Stiky menu

Mesajde indexcafea » Mie Apr 03, 2013 2:52 pm

Andrei HAINAROSIE scrie:

Cod: Selectaţi tot

<script>
$(function() {

   // grab the initial top offset of the navigation
   var sticky_navigation_offset_top = $('#sticky_navigation').offset().top;
   
   // our function that decides weather the navigation bar should have "fixed" css position or not.
   var sticky_navigation = function(){
      var scroll_top = $(window).scrollTop(); // our current vertical position from the top
     
      // if we've scrolled more than the navigation, change its position to fixed to stick to top, otherwise change it back to relative
      if (scroll_top > sticky_navigation_offset_top) {
         $('#sticky_navigation').css({ 'position': 'fixed', 'top':0, 'left':0 });
if (!$('#sticky_navigation').hasClass('fixed-menu')) {
  $('#sticky_navigation').addClass('fixed-menu');
}
      } else {
if ($('#sticky_navigation').hasClass('fixed-menu')) {
  $('#sticky_navigation').removeClass('fixed-menu');
}
         $('#sticky_navigation').css({ 'position': 'relative' });
      }   
   };
   
   // run our function on load
   sticky_navigation();
   
   // and run it again every time you scroll
   $(window).scroll(function() {
       sticky_navigation();
   });
   
   // NOT required:
   // for this demo disable all links that point to "#"
   $('a[href="#"]').click(function(event){
      event.preventDefault();
   });
   
});
</script>


Daca schimbi js-ul tau cu acesta, o sa vezi ca div-ul cu id-ul "sticky_navigation" va mai primi o clasa "fixed-menu" atunci cand dai scroll in josul paginii.
Clasa respectiva va fi numai cand dai scroll in jos; te poti folosi de ea in fisierul tau de css (demo.css).


Multumesc! am reusit, rezultatul aici: mvc.anuntul10.ro

Avatar utilizator
Andrei HAINAROSIE
PHPRomania Supporter
Mesaje: 15
Membru din: Joi Oct 18, 2012 8:20 pm
Localitate: Bucuresti
Contact:

Re: Implementare Stiky menu

Mesajde Andrei HAINAROSIE » Mie Apr 03, 2013 3:16 pm

Cu placere!

indexcafea
Average Member
Mesaje: 65
Membru din: Mie Noi 07, 2012 9:18 pm
Contact:

Re: Implementare Stiky menu

Mesajde indexcafea » Joi Apr 04, 2013 6:43 pm

Andrei HAINAROSIE scrie:Cu placere!


Mai am nevoie de putin ajutor :( stiu ca ar trebui sa ma straduiesc mai mult sa invat insa sunt incepator si nu pot sa invat 10 limbaje de programare odata, am inceput cu PHP, apoi o sa continui si cu javascript.

As vrea atunci cand meniul devine fixed sa adaug un div la inceputul meniului (in acest div vreau sa pun un logo)

Avatar utilizator
Andrei HAINAROSIE
PHPRomania Supporter
Mesaje: 15
Membru din: Joi Oct 18, 2012 8:20 pm
Localitate: Bucuresti
Contact:

Re: Implementare Stiky menu

Mesajde Andrei HAINAROSIE » Joi Apr 04, 2013 6:54 pm

in if-ul in care se adauga clasa "fixed-menu", adauga:

Cod: Selectaţi tot

jQuery('#sticky_navigation_wrapper').prepend('<div id="logo"></div>');


si unde o scoti

Cod: Selectaţi tot

jQuery('#logo').remove();

indexcafea
Average Member
Mesaje: 65
Membru din: Mie Noi 07, 2012 9:18 pm
Contact:

Re: Implementare Stiky menu

Mesajde indexcafea » Joi Apr 04, 2013 8:57 pm

Andrei HAINAROSIE scrie:in if-ul in care se adauga clasa "fixed-menu", adauga:

Cod: Selectaţi tot

jQuery('#sticky_navigation[color=#FF4000]_wrapper[/color]').prepend('<div id="logo"></div>');


si unde o scoti

Cod: Selectaţi tot

jQuery('#logo').remove();


Am adaugat codul cu o mica modificare (am scos ce e scris cu rosu mai sus)
Nu stiu daca ai scris din greseala sau nu ideea e ca nu afisa nimic. Acum afiseaza poti vedea exemplul aici: http://mvc.anuntul10.ro/

Insa nu adauga div-ul unde trebuie, sincer nici nu stiu cum sa cer rezolvarea

Cod: Selectaţi tot

Am incercat asa:   jQuery('.demo_container').prepend('<div id="logo">LOGO</div>');


Imi afiseaza div-ul log in fata meniului aliniat corect, dar daca dau scroll de mai multe ori se multiplica div-ul


Înapoi la “HTML/JavaScript/CSS”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 5 vizitatori