using jquery to link dropdown to anchor text

前端 未结 3 976
青春惊慌失措
青春惊慌失措 2021-01-21 04:43

I this select dropdown on my page


                        
    
提交评论

  • 2021-01-21 05:29

    You can use animate like this to scroll to selected element in animated fashion:

    <script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery("#select1").change(function(){
            var jump = jQuery("#select1").val();
            var new_position = jQuery('#job'+jump).offset();
            $('body, html').animate({scrollTop, jQuery('#job'+jump).offset().top})
            return false;
        });​
    }
    </script>
    
    0 讨论(0)
  • 2021-01-21 05:47

    This easy one workes for me :

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>  
    <script type="text/javascript">
        function scrollTo(target){
            var targetPosition = $(target).offset().top;
            $('html,body').animate({ scrollTop: targetPosition}, 'slow');
        }
    </script>
    

    Now you can use the onChange event to to execute the function:

    <select name="dropdpown" size="1" onChange="scrollTo(this.value)">
        <option value="#link">text</option>
    </select>
    

    If you provide a correct link like this everything works fine

    <div id="link"> ...
    

    It took me a while because I am not used to javascript, but finally found a short and easy solution that works.

    Greetiing mz

    0 讨论(0)
  • 提交回复
    热议问题