如何使用jQuery向上或向下滾動頁面到錨點?我正在尋找一種方法來包含幻燈片效果,當您單擊頁面上或下的本地錨點鏈接時。我想要一個你有這樣一個鏈接的東西:<a href="#nameofdivetc">link text, img etc.</a>也許添加了一個類,所以你知道你希望這個鏈接是一個滑動鏈接:<a href="#nameofdivetc" class="sliding-link">link text, img etc.</a>然后,如果單擊此鏈接,頁面將向上或向下滑動到所需位置(可以是div,標題,頁面頂部等)。這就是我以前的情況: $(document).ready(function(){
$(".scroll").click(function(event){
//prevent the default action for the click event
event.preventDefault();
//get the full url - like mysitecom/index.htm#home
var full_url = this.href;
//split the url by # and get the anchor target name - home in mysitecom/index.htm#home
var parts = full_url.split("#");
var trgt = parts[1];
//get the top offset of the target anchor
var target_offset = $("#"+trgt).offset();
var target_top = target_offset.top;
//goto that anchor by setting the body scroll top to anchor top
$('html, body').animate({scrollTop:target_top}, 1500, 'easeInSine');
});});
- 3 回答
- 0 關注
- 762 瀏覽
添加回答
舉報
0/150
提交
取消