开发者

scrollTO with strings

I'm trying to do a scrollTO function with my code bu开发者_如何学编程t I keeps just popping up to the top like its not connecting to the id im telling it to go to is there something im doing wrong?.

<div class="a-z">
<? $a1=range("A","Z");
    foreach($a1 as $char2){
    echo "<a href='#$char2' onclick='$.scrollTo( '#$char2', 800, {easing:'elasout'} ); title='$char2'>$char2</a>";
    }?> 

    <script type="text/javascript">
$(document).ready('#<?php echo $char2 ?>').localScroll({
   target:'<?php echo $char2 ?>'
});
</script>
</div>

code updated i dont get any errors


<? $a1=range("A","Z");
    foreach($a1 as $char2){
    echo "<a href='#' onclick='$.scrollTo( \'#{$char2}\', 800, {easing:\'elasout\'} );' title='{$char2}'>{$char2}</a>";
    }?> 

Try this one as the php variable "$char2" in single quotes is not getting its value it should be enclosed in {} :)


rahul was almost right, but you need to use escaped double quotes inside of the onclick's single quotes. Escape them so the php parser doesn't think you are ending the 'echo' argument.

<? $a1=range("A","Z");
    foreach($a1 as $char2){
    echo "<a href='#' onclick='$.scrollTo( \"#{$char2}\", 800, {easing:\"elasout\"} );' title='{$char2}'>{$char2}</a>";
}?> 

A better solution is giving all of those links a class and a custom data attribute, then using jQuery to assign a click handler:

<? $a1=range("A","Z");
    foreach($a1 as $char2){
    echo "<a href='#' class='linkClass' data-destination='#{$char2}' title='{$char2}'>{$char2}</a>";
}?> 

In a script tag:

$(document).ready( function() {
    $(".linkClass").click( function() {
        //On a click event, get the destination data attribute, then scroll to it
        $.scrollTo( $(this).data("destination"), 800, {easing: "elasout"});
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜