Spdexter
Spdexter

Reputation: 933

functions - passing element id as parameter

http://jsfiddle.net/vidhyaakrish/AFm89/64/

I am trying to pass an element id (eg.#target-elemet as parameter to scroll_to(element))

function scroll_to(element){
    $('html, body').animate({
        scrollTop: $(element).offset().top
    },1000);
    console.log(element);
}

scroll_to('"\#target-element\"');

but i am not able to achieve - Can somebody help me?

Upvotes: 0

Views: 70

Answers (4)

meni181818
meni181818

Reputation: 1101

you can do it by creating a little jquery plugin:

$.fn.animateScrollToElement = function(speed, marginTop) {
    marginTop = marginTop || 0; // if not set, will be 0
    $('html, body').animate({
        scrollTop: $(this).offset().top - marginTop
    }, speed);
    return this;
}

usage:

// element                        speed, 'margin top'
$( selector ).animateScrollToElement(1000, 20);

then you can use Standard jquery selector, the plugin function will handle the element by this.

demo: http://jsfiddle.net/AFm89/67/

Upvotes: 0

Dennis Pietersma
Dennis Pietersma

Reputation: 11

Your selector for the ID is not recognized by JQuery. (Here the # selector for the id)

Changing your code to this should help:

scroll_to('#target-element');

For some more insight on JQuery Selectos, see this link: JQuery Selectors

Link to your updated Fiddle: JSFiddle

Upvotes: 1

Kushal
Kushal

Reputation: 1360

scroll_to("#target-element");

this should do it

Upvotes: 1

Sudharsan S
Sudharsan S

Reputation: 15393

change

scroll_to('"\#target-element\"');

to

scroll_to('#target-element');

DEMO

Upvotes: 1

Related Questions