下载文件并在链接点击时滚动到底部div

时间:2015-03-25 15:57:46

标签: javascript jquery html css button

点击链接,我希望用户下载文件(在href中)。在同一次点击的同时,我想将它们向下滚动到页面上。

根据我的代码,我只能做一个或另一个。我无法执行这两项操作。

HTML

<a href="http://www.cdc.gov/animalimportation/images/dog2.jpg" download="dog2.jpg"
    class="scroll-to-bottom-link">Scroll to bottom</a>
<p>here is some secondary information</p>

的jQuery

$('.scroll-to-bottom-link').click(function(){
$('html, body').animate({scrollTop : $('body').height() }, 800);
return false;
});

关于如何做到这一点的任何想法?我发现的其他选项使用href="#",但我需要下载。

2 个答案:

答案 0 :(得分:1)

您必须删除return false,因为它会调用阻止下载的preventDefault()

这就足够了:

$('.scroll-to-bottom-link').click(function(){
   $('html, body').animate({scrollTop : $('body').height() }, 800);
});

DEMO

答案 1 :(得分:1)

我会在div标记中添加a并将事件附加到其中

   <a href="http://www.cdc.gov/animalimportation/images/dog2.jpg" download ><div class="scroll-to-bottom-link">Scroll to bottom</div></a>
    <p>here is some secondary information</p>

然后从函数中删除remove false

$('.scroll-to-bottom-link').click(function(){
    $('html, body').animate({scrollTop : $('body').height() }, 800);
});

JSFiddle

相关问题