简体   繁体   English

在MVC3中使用Javascript垂直滚动div

[英]Scrolling a div vertically using Javascript in MVC3

I am an application development intern. 我是应用程序开发实习生。 I'm making an application using ASP.NET MVC3 that displays in boxes various projects the company using it is working on. 我正在使用ASP.NET MVC3制作应用程序,该应用程序在框中显示公司正在使用的各种项目。 These boxes are created in a table using a foreach loop. 这些框是使用foreach循环在表中创建的。

    <div class= "row">
       @foreach (var item in Model)
       {
       @Html.Action("Client", new { controller = "Dashboard", Client = item })      
       } 
    </div>

I want these boxes to automatically scroll down if there is an overflow. 如果溢出,我希望这些框自动向下滚动。 This appears to be the function that I would like to use: 这似乎是我要使用的功能:

    function pageScroll() {
       window.scrollBy(0,50); // horizontal and vertical scroll increments
       scrolldelay = setTimeout('pageScroll()',100); // scrolls every 100 milliseconds
    }

How do I apply this function to the boxes being created in the foreach loop? 如何将此功能应用于在foreach循环中创建的框?

EDIT: Using Timothy-Strimple's advice, I used a Jquery plugin called scrollTo() to create this: 编辑:根据Timothy-Strimple的建议,我使用了一个名为scrollTo()的Jquery插件来创建此插件:

for (d = 0; d < 52; d) {

   $(".dashboard-well").scrollTo('100%', 10000).delay(2000);
   $(".dashboard-well").scrollTo('0%', 10000).delay(2000);
   d++;
 }

Now the div I was looking at, when overflowed, will scroll down, then back up 5x. 现在,当我看到的div溢出时,它将向下滚动,然后向上滚动5倍。

I'm not quite sure I understand what you're trying to do. 我不太确定我了解您要做什么。 If you want to set a div to overflow: scroll, and then scroll to the bottom of it, that's pretty straight forward with jQuery. 如果要将div设置为溢出,请滚动:滚动,然后滚动到它的底部,这对于jQuery非常简单。 I have done something like that on my sample chat app where the page will scroll to the bottom every time there is a new message. 我在示例聊天应用程序上做了类似的操作,每当有新消息出现时,页面都会滚动到底部。 It looks like this: 看起来像这样:

$('#messages').scrollTop($('#messages')[0].scrollHeight);

So what you can do is after you've created all of your rows, set scrollTop on the element like I have done above, and set it to the scrollHeight and that will put the scrollbar at the bottom of the section. 因此,您可以做的是在创建所有行之后,像上面一样在元素上设置scrollTop,然后将其设置为scrollHeight,这会将滚动条放在该部分的底部。

If that is not what you are asking, please update your question or post a comment so I can update my answer. 如果这不是您要的内容,请更新您的问题或发表评论,以便我更新我的答案。

使用javascript显示/隐藏<div>滚动时</div><div id="text_translate"><p>我想制作一个采用性的粘性导航栏。 我以前从未用 Javascript 编写过自己的代码。</p><p> 我的解决方案是制作两个基于滚动显示/隐藏的粘性导航栏。 我的解决方案是这样的:</p><ul><li> 首先,在完成任何滚动之前,我的第一个导航栏(黄色)将可见并位于距顶部 100px 的位置,即top:100px 。</li><li> 然后,当启动滚动时,我希望这个黄色&lt;div&gt;使用display:none消失。</li><li> 同样在滚动时,在黄色导航栏消失的同时,我会显示带有top:200px的橙色导航栏。</li><li> 现在,橙色条从一开始就(错误地)显示,但它不应该......在任何给定时间只应显示一个条。 我希望栏,即两个&lt;div&gt;元素,在多次上下滚动时也能保持出现/消失。</li></ul><p> 我的 javascript 有问题,codepen 抱怨$未定义。</p><p> <a href="https://codepen.io/G1111/pen/RwBPrPR" rel="nofollow noreferrer">https://codepen.io/G1111/pen/RwBPrPR</a> </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $(window).scroll(function() { Testvariable = $(window).scrollTop(); if (Testvariable == 0) { document.getElementById("stickys").style.display = "normal"; document.getElementById("stickys2").style.display = "none"; } else { document.getElementById("stickys").style.display = "none"; document.getElementById("stickys2").style.display = "normal"; } });</pre><pre class="snippet-code-css lang-css prettyprint-override"> #stickys { top: 100px;important: background-color; yellow: height; 100px: position; fixed: position; fixed:important; box-sizing: border-box; margin: 0 0% 0 0%;important: padding; 0:important; width: calc(60vw - 0%); left: calc(20vw - 0%); right: calc(20vw - 0%); width: calc(100vw - 0%); left: 0px; right: 0px; opacity: 1; } #stickys2 { top: 200px;important: background-color; orange: height; 100px: position; fixed: position; fixed:important; box-sizing: border-box; margin: 0 0% 0 0%;important: padding; 0:important; width: calc(60vw - 0%); left: calc(20vw - 0%); right: calc(20vw - 0%); width: calc(100vw - 0%); left: 0px; right: 0px; opacity: 1; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="stickys"&gt; My first sticky navigation bar. Show initially, then hide when slighest scroll, that is when Testvariable&gt;0. Here, top:100px &lt;/div&gt; &lt;div id="stickys2"&gt; My second sticky navigation bar. Hide initially, then show when slighest scroll, that is when Testvariable&gt;0. Here, top: 200px. &lt;/div&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;</pre></div></div><p></p></div> - Using javascript to show/hide <div> when scrolling

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用javascript垂直居中div - using javascript to vertically center a div 在MVC3上使用javascript重定向的奇怪行为 - Weird behavior on redirecting using javascript on MVC3 在主页或子页面的MVC3中使用javascript - Using javascript in MVC3, in master or child page 使用javascript在MVC3中验证表单 - validating form in MVC3 using javascript 使用Javascript将Div与Button垂直居中 - Vertically center Div with Button using Javascript 具有Javascript的RadioButton(MVC3) - RadioButton with Javascript (MVC3) MVC3中的JavaScript处理 - Javascript Handling in MVC3 使用香草 JavaScript 滚动时显示 div - Show div when scrolling using vanilla JavaScript 使用说Javascript以编程方式滚动Div - Scrolling a Div programatically using say Javascript 使用javascript显示/隐藏<div>滚动时</div><div id="text_translate"><p>我想制作一个采用性的粘性导航栏。 我以前从未用 Javascript 编写过自己的代码。</p><p> 我的解决方案是制作两个基于滚动显示/隐藏的粘性导航栏。 我的解决方案是这样的:</p><ul><li> 首先,在完成任何滚动之前,我的第一个导航栏(黄色)将可见并位于距顶部 100px 的位置,即top:100px 。</li><li> 然后,当启动滚动时,我希望这个黄色&lt;div&gt;使用display:none消失。</li><li> 同样在滚动时,在黄色导航栏消失的同时,我会显示带有top:200px的橙色导航栏。</li><li> 现在,橙色条从一开始就(错误地)显示,但它不应该......在任何给定时间只应显示一个条。 我希望栏,即两个&lt;div&gt;元素,在多次上下滚动时也能保持出现/消失。</li></ul><p> 我的 javascript 有问题,codepen 抱怨$未定义。</p><p> <a href="https://codepen.io/G1111/pen/RwBPrPR" rel="nofollow noreferrer">https://codepen.io/G1111/pen/RwBPrPR</a> </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $(window).scroll(function() { Testvariable = $(window).scrollTop(); if (Testvariable == 0) { document.getElementById("stickys").style.display = "normal"; document.getElementById("stickys2").style.display = "none"; } else { document.getElementById("stickys").style.display = "none"; document.getElementById("stickys2").style.display = "normal"; } });</pre><pre class="snippet-code-css lang-css prettyprint-override"> #stickys { top: 100px;important: background-color; yellow: height; 100px: position; fixed: position; fixed:important; box-sizing: border-box; margin: 0 0% 0 0%;important: padding; 0:important; width: calc(60vw - 0%); left: calc(20vw - 0%); right: calc(20vw - 0%); width: calc(100vw - 0%); left: 0px; right: 0px; opacity: 1; } #stickys2 { top: 200px;important: background-color; orange: height; 100px: position; fixed: position; fixed:important; box-sizing: border-box; margin: 0 0% 0 0%;important: padding; 0:important; width: calc(60vw - 0%); left: calc(20vw - 0%); right: calc(20vw - 0%); width: calc(100vw - 0%); left: 0px; right: 0px; opacity: 1; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="stickys"&gt; My first sticky navigation bar. Show initially, then hide when slighest scroll, that is when Testvariable&gt;0. Here, top:100px &lt;/div&gt; &lt;div id="stickys2"&gt; My second sticky navigation bar. Hide initially, then show when slighest scroll, that is when Testvariable&gt;0. Here, top: 200px. &lt;/div&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;</pre></div></div><p></p></div> - Using javascript to show/hide <div> when scrolling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM