Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   правильно задать условие (https://javascript.ru/forum/misc/49140-pravilno-zadat-uslovie.html)

new_developer 30.07.2014 21:02

правильно задать условие
 
Посоветуйте что можно сделать с якорем, скрипт выглядит так :


//scroll
 
    var theDestinations = $('a[id]');
 
    theDestinations.each(function(i){
 
        var thisDestination =  $(this),
            thisDestinationOffset = thisDestination.offset(),
 
            thisLink = $("a[href='/#" + thisDestination.attr("id") + "']");
 
 
        if(thisLink.length > 0) {
            thisLink.click(function(){
                $('html,body').animate({scrollTop : thisDestinationOffset.top-10}, 1500);
                return false;
            });
        }
    });


2
3
thisLink = $("a[href='/#" + thisDestination.attr("id") + "']"); 
 
// В этой строке слеш перед решёткой чтобы с других разделов сайта на главную ссылалось нормально.. Но проблема когда с главной пробую ссылатся на якорь он его невидит едит до конца страницы. На главной должно быть без слеша, но скрипт под слеш заточен, как быть?)


так постоянно перезагружается браузер


if(location.href="http://testsite.ru/"){
 
        var theDestinations = $('a[id]');
 
        theDestinations.each(function(i){
 
            var thisDestination =  $(this),
                thisDestinationOffset = thisDestination.offset(),
 
                thisLink = $("a[href='#" + thisDestination.attr("id") + "']");
 
 
            if(thisLink.length > 0) {
                thisLink.click(function(){
                    $('html,body').animate({scrollTop : thisDestinationOffset.top-10}, 1500);
                    return false;
                });
            }
        });
 
    } else {
        var theDestinations = $('a[id]');
 
        theDestinations.each(function(i){
 
            var thisDestination =  $(this),
                thisDestinationOffset = thisDestination.offset(),
 
                thisLink = $("a[href='/#" + thisDestination.attr("id") + "']");
 
 
            if(thisLink.length > 0) {
                thisLink.click(function(){
                    $('html,body').animate({scrollTop : thisDestinationOffset.top-10}, 1500);
                    return false;
                });
            }
        });
    }

Rise 30.07.2014 21:10

new_developer, location.href == "http://testsite.ru/"


Часовой пояс GMT +3, время: 04:59.