滚动时根据其他元素显示/隐藏(切换)固定元素?(Show/Hide (Toggle) a Fixed Element Depending on Other Elements When Scrolling?)
是否有一种方法可以在显示和隐藏固定元素时根据它在垂直滚动时传递的元素进行切换?
以下是我期望实现的视觉参考:
我更喜欢这个功能来检测页面上固定元素的位置,而不是窗口的滚动位置。
希望照片足够说明问题; 否则,请查看下面的代码片段:
$(document).ready(function() { var $window = $(window); var div2 = $('#div2'); var div1 = $('#div1'); var div1_top = div1.offset().top; var div1_height = div1.height(); var div1_bottom = div1_top + div1_height; console.log(div1_bottom); $window.on('scroll', function() { var scrollTop = document.documentElement.scrollTop; var viewport_height = $window.height(); var scrollTop_bottom = scrollTop + viewport_height; div2.toggleClass('show', scrollTop > div1_top && (scrollTop + window.innerHeight) < div1_bottom); }); });
body { background: #ccffcc; padding: 0; margin: 0; border: 0; text-align: center; } #div1 { background: #0099ff; height: 1500px; color: #fff; } #div2 { width: 100px; height: 100px; text-align: center; position: fixed; margin: auto; top: 0; left: 0; bottom: 0; right: 0; background: #ffff00; color: #000; display: none; } #div2.show { display: block; } #div3 { height: 1500px; color: #000; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <br> <br> <br> <br> <br> Scroll area before <b>div1</b> appears <br> <br> <br> <br> <br> <br> <div id="div1"> <div id="div2">This is <b>div2</b></div> This is <b>div1</b> <br> <i>(Toggle show/hide <b>div2</b> when the top of <b>div2</b> passes the top of <b>div1</b>)</i> </div> <div id="div3"> This is <b>div3</b> <br> <i>(Toggle show/hide <b>div2</b> when the bottom of <b>div2</b> reaches the top of <b>div3</b>)</i> </div>
Is there a way to toggle between showing and hiding a fixed element based on the elements it passes when scrolling vertically?
Here is a visual reference of what I'm looking to achieve:
I would prefer for this to function to detect the fixed element's position on a page, rather than the window's scroll position.
Hopefully the photo is explanatory enough; otherwise, please check the snippet below:
$(document).ready(function() { var $window = $(window); var div2 = $('#div2'); var div1 = $('#div1'); var div1_top = div1.offset().top; var div1_height = div1.height(); var div1_bottom = div1_top + div1_height; console.log(div1_bottom); $window.on('scroll', function() { var scrollTop = document.documentElement.scrollTop; var viewport_height = $window.height(); var scrollTop_bottom = scrollTop + viewport_height; div2.toggleClass('show', scrollTop > div1_top && (scrollTop + window.innerHeight) < div1_bottom); }); });
body { background: #ccffcc; padding: 0; margin: 0; border: 0; text-align: center; } #div1 { background: #0099ff; height: 1500px; color: #fff; } #div2 { width: 100px; height: 100px; text-align: center; position: fixed; margin: auto; top: 0; left: 0; bottom: 0; right: 0; background: #ffff00; color: #000; display: none; } #div2.show { display: block; } #div3 { height: 1500px; color: #000; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <br> <br> <br> <br> <br> Scroll area before <b>div1</b> appears <br> <br> <br> <br> <br> <br> <div id="div1"> <div id="div2">This is <b>div2</b></div> This is <b>div1</b> <br> <i>(Toggle show/hide <b>div2</b> when the top of <b>div2</b> passes the top of <b>div1</b>)</i> </div> <div id="div3"> This is <b>div3</b> <br> <i>(Toggle show/hide <b>div2</b> when the bottom of <b>div2</b> reaches the top of <b>div3</b>)</i> </div>
原文:https://stackoverflow.com/questions/50710816
最满意答案
如果您只想让函数退出,请使用
return
。 但是,它不是表达式,因此您需要将其更改为if
语句:if(!isset($_GET['type'])) { return; } $type = $_GET['type'];
如果你确实希望你的整个页面停止执行,那么是的,这是正确的(但为了清晰起见,我仍然会使用
if
)。If you just want your function to exit, use
return
. However, it's not an expression, so you'll need to change it to anif
statement:if(!isset($_GET['type'])) { return; } $type = $_GET['type'];
If you do want your whole page to stop execution, then yes, that's correct (but I'd still use an
if
for clarity).
相关问答
更多-
exit,exit(),exit(0),die(),die(0) - 如何退出脚本(exit, exit(), exit(0), die(), die(0) - How to exit script)[2023-01-24]
这些都是一样的。 我很确定die()只是一个直立的别名exit() ,但即使不是,它仍然是相同的。 当这些函数之一被赋予一个字符串参数时,它会在终止进程之前打印出该字符串。 当它遇到255以下的整数时,该整数被认为是进程的返回代码,它被传递回调用PHP脚本的进程。 这在编写命令行应用程序(PHP不是仅Web!!)时特别有用。 只要exit , exit()和exit(0)之间的区别就是没有。 前两者绝对没有区别,因为exit在技术上是一种语言构造,而不是一个函数,因此可以使用或不带括号来调用,就像echo一 ... -
在这里组合文档,替换和死亡时消除中间变量(Eliminate intermediate variable when combining here document, substitution and die)[2023-04-11]
使用perl 5.14或更新版本,你可以, die <<'END_MSG' My super error message Which happens to span over multiple lines But that I want to print without newlines. END_MSG =~ s/\n//gr; 编辑:或 die <<'END_MSG' =~ s/\n//gr .. END_MSG With perl 5.14 or newer you can, die <<'END ... -
如何不死?(How to not die?)[2022-07-15]
or print mysql_error(); 尽管将它打印出来几乎就像死亡一样糟糕。 在完美的世界中,它会是 $res = mysql_query($query) or trigger_error(mysql_error().$query); 总是这样做。 你不仅会得到文件名和行号(当你在页面上有很多查询时这很有帮助),而且还会将敏感信息保密在生产服务器上,在那里你将关闭显示错误和登录。 or print mysql_error(); though printing it out is almo ... -
$('.dialog').delegate('.please_only_onetime', 'focusin', function () { //this is all you need to do... no rebinding, no "die"'ing ... }); 或者为了简单起见,您可以继续使用。 我认为你应该切换到delegate() 。 $('.dialog').delegate('.please_only_onetime', 'focusin', function () { // ...
-
如果您只想让函数退出,请使用return 。 但是,它不是表达式,因此您需要将其更改为if语句: if(!isset($_GET['type'])) { return; } $type = $_GET['type']; 如果你确实希望你的整个页面停止执行,那么是的,这是正确的(但为了清晰起见,我仍然会使用if )。 If you just want your function to exit, use return. However, it's not an expression, so you ...
-
实际上,我找到了一个答案来维护网址,而不是重定向: $data['main_content'] = 'unauthorized_access'; $this->load->view('includes/template', $data); // Force the CI engine to render the content generated until now $this->CI =& get_instance(); $this->CI->output->_display(); die( ...
-
public function actionCreate() { $model = new Opportunity(); if ($model->load(Yii::$app->request->post())) { echo(" "); var_dump($model->project_control_id); ...
-
从die()获得无返回值(Getting no return value from die())[2023-10-11]
如果status是整数,则该值将用作退出状态而不打印。 http://php.net/manual/en/function.exit.php 所以我养成了这样做的习惯 var_dump($count);die; 因为如果值不可打印, var_dump()仍然可以给你一些提示。 If status is an integer, that value will be used as the exit status and not printed. http://php.net/manual/en/functi ... -
根据定义,静态变量不是实例变量,因此在同一个类的所有实例中始终相等。 Static variables, by definition, are not instance variables, and will therefore always be equal across all instances of the same class.
-
这里不需要dump-autoload,因为你在psr-4 confiugred文件夹app创建了一个新的命名空间。 解决方案是将文件夹billing重命名为大写情况,因为laravel使用遵循psr-4标准的composer autoload,该标准声明命名空间以区分大小写的方式匹配文件夹名称。 注意:作曲家提供的替代自动加载方案可能需要编辑composer dump-autoload或在创建新文件时编辑composer.json There is no need here for dump-autoloa ...