Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: jak zrobić podświetlenie aktualnie wybranej pozycji w menu
Forum PHP.pl > Forum > Po stronie przeglądarki > JavaScript
wazi20
Witam

Zwracam się z prośbą. Czy moglibyście mi pomóc w małej modyfikacji skryptu.
Chciałbym, aby po wybraniu jakiejś pozycji z menu została ona podświetlona na jakiś inny kolor. Bo w tej chwili nie wiadomo co aktualnie zostało wybrane.

Z góry dziękuję.
Pozdrawiam

Kod
var remember = true; //Remember menu states, and restore them on next visit.
var contractall_default = true; //Should all submenus be contracted by default? (true or false)

var menu, titles, submenus, arrows, bypixels;
var heights = new Array();

var n = navigator.userAgent;
if(/Opera/.test(n)) bypixels = 2;
else if(/Firefox/.test(n)) bypixels = 3;
else if(/MSIE/.test(n)) bypixels = 2;

/////DD added expandall() and contractall() functions/////

function slash_expandall(){
if (typeof menu!="undefined"){
    for(i=0; i<Math.max(titles.length, submenus.length); i++){
        titles[i].className="title";
        arrows[i].src = "slashfiles/expanded.gif";
        submenus[i].style.display="";
        submenus[i].style.height = heights[i]+"px";
    }
}
}

function slash_contractall(){
if (typeof menu!="undefined"){
    for(i=0; i<Math.max(titles.length, submenus.length); i++){
        titles[i].className="titlehidden";
        arrows[i].src = "slashfiles/collapsed.gif";
        submenus[i].style.display="none";
        submenus[i].style.height = 0;
    }
}
}


/////End DD added functions///////////////////////////////


function init(){
    menu = getElementsByClassName("sdmenu", "div", document)[0];
    titles = getElementsByClassName("title", "span", menu);
    submenus = getElementsByClassName("submenu", "div", menu);
    arrows = getElementsByClassName("arrow", "img", menu);
    for(i=0; i<Math.max(titles.length, submenus.length); i++) {
        titles[i].onclick = gomenu;
        arrows[i].onclick = gomenu;
        heights[i] = submenus[i].offsetHeight;
        submenus[i].style.height = submenus[i].offsetHeight+"px";
    }
    if(remember)
                restore()
        else if (contractall_default) //DD added code
                slash_contractall() //DD added code
}

function restore() {
    if(getcookie("menu") != null) {
        var hidden = getcookie("menu").split(",");
        for(var i in hidden) {
            titles[hidden[i]].className = "titlehidden";
            submenus[hidden[i]].style.height = "0px";
            submenus[hidden[i]].style.display = "none";
            arrows[hidden[i]].src = "slashfiles/collapsed.gif";
        }
    }
}

function gomenu(e) {
    if (!e)
        var e = window.event;
    var ce = (e.target) ? e.target : e.srcElement;
    var sm;
    for(var i in titles) {
        if(titles[i] == ce || arrows[i] == ce)
            sm = i;
    }
    if(parseInt(submenus[sm].style.height) > parseInt(heights[sm])-2) {
        hidemenu(sm);
    } else if(parseInt(submenus[sm].style.height) < 2) {
        titles[sm].className = "title";
        showmenu(sm);
    }
}

function hidemenu(sm) {
    var nr = submenus[sm].getElementsByTagName("a").length*bypixels;
    submenus[sm].style.height = (parseInt(submenus[sm].style.height)-nr)+"px";
    var to = setTimeout("hidemenu("+sm+")", 5);
    if(parseInt(submenus[sm].style.height) <= nr) {
        clearTimeout(to);
        submenus[sm].style.display = "none";
        submenus[sm].style.height = "0px";
        arrows[sm].src = "slashfiles/collapsed.gif";
        titles[sm].className = "titlehidden";
    }
}

function showmenu(sm) {
    var nr = submenus[sm].getElementsByTagName("a").length*bypixels;
    submenus[sm].style.display = "";
    submenus[sm].style.height = (parseInt(submenus[sm].style.height)+nr)+"px";
    var to = setTimeout("showmenu("+sm+")", 5);
    if(parseInt(submenus[sm].style.height) > (parseInt(heights[sm])-nr)) {
        clearTimeout(to);
        submenus[sm].style.height = heights[sm]+"px";
        arrows[sm].src = "slashfiles/expanded.gif";
    }
        
        
}

function store() {
    var hidden = new Array();
    for(var i in titles) {
        if(titles[i].className == "titlehidden")
            hidden.push(i);
    }
    putcookie("menu", hidden.join(","), 5);
}

function getElementsByClassName(strClassName, strTagName, oElm){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }  
    }
    return (arrReturnElements)
}

function putcookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate);
}

function getcookie(c_name) {
    if(document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(c_name + "=");
        if(c_start != -1) {
            c_start = c_start + c_name.length + 1;
            var c_end = document.cookie.indexOf(";",c_start);
            if(c_end == -1)
                c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return null;
}

window.onload = init;
if(remember) window.onunload = store;


w html

Kod
    <div class="sdmenu">
      
      <span class="title"><img src="slashfiles/expanded.gif" class="arrow" alt="-" />Menu1</span>
      <div class="submenu">
          
        <a href="#">Menu1</a>
        <a href="#">Menu2</a>
        <a href="#">Menu3</a>
        <a href="#">Menu4</a>
        <a href="#">Menu5</a>
        
        </div>
    </div>
Pawel_W
  1. #submenu a:hover{
  2. background: #000; // kolor
  3. }
wazi20
Niestety takie rozwiązanie nie działa

Kod
.sdmenu {
    width: 257px;
    font-family: Sans-Serif;
    font-size: 12px;
    padding-bottom: 10px;

    color: #FFF;
}

.sdmenu .title, .sdmenu .titlehidden{
    display: block;
    padding: 5px 0;
    font-weight: bold;
    color: black;
    background: #FFF url(title.gif) repeat-x;
    text-align:
}

.sdmenu .title {
    border-bottom: 1px solid #DDD;
}

.sdmenu .arrow {
        margin-left: 10px;
    margin-right: 7px;
}

.sdmenu .titlehidden {
    border-bottom: none;
}

.sdmenu #top {
    background: url(toptitle.gif) no-repeat;
}

.sdmenu .submenu {
    overflow: hidden;
}

.sdmenu .submenu a {
    padding: 5px 0;
    text-indent: 10px;
    background: #FFF;
    display: block;
    border-bottom: 1px solid #DDD;
    color: #066;
    text-decoration: none;
}

.sdmenu .submenu a:hover {
    background : #066 url(linkarrow.gif) no-repeat right center;
    color: #FFF;
}
.sdmenu .submenu a:active {
    background : #000000;

}
Pawel_W
  1. background : #066 url(linkarrow.gif) no-repeat right center;

nie może być right center, jak już to right middle
erix
http://www.w3.org/TR/CSS21/colors.html#pro...ground-position

Nie wprowadzaj w błąd.
frok
Cytat
<a href="#">Menu1</a>

zmień na:
<a href="#" class="zamiana">Menu1</a>

w stylach dopisz:


a.zamiana, a.zamiana:visited {
text-decoration: none; //usuwa podkreślenie
color: #fff; //kolor czcionki
font-weight:bold; //pogrubienie, jezeli nie potrzebne to usun linijke

}

a.zamiana:hover {
text-decoration: none;
color: #000; //kolor po najechaniu

}
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.