如何解决在滚动事件中仅一次运行if语句
我有这段代码,我只尝试执行一次if语句。 因此,用户在转到页面中间时只会收到一个警报显示,跳过警报并继续滚动后,他就不会再收到警报。 因此我添加了此PassedValue布尔变量,但是在显示警报时不会将其更改为true。
$f = $.noConflict();
$f(document).ready(function() {
$f(window).scroll(function() {
var scrollAmount = $f(window).scrollTop();
var documentHeight = $f(document).height();
var scrollPercent = (scrollAmount / documentHeight) * 100;
var PassedValue = false;
//$(".3amal").append(scrollPercent);
if (scrollAmount > documentHeight * 0.3 && !PassedValue) {
alert('the user scrolled once!');
PassedValue = true;
} else {
//alert('Nothing done nothing to save');
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="container">
<div id="sidebar">
<ul>
<li><a id="aboutlink" href="#" style="position: sticky;top: 0;">auck</a></li>
<li><a id="projectslink" href="#">Projects</a></li>
<li><a id="resumelink" href="#">Resume</a></li>
<li><a id="contactlink" href="#">Contact</a></li>
</ul>
</div>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<div class="3amal">Hellow i'm here</div>
</div><br/><br/><br/>
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<div id="content">
<div class="" id="about">
<p class="header">uck</p>
<p class="info">Lorem ipsum dolor sit amet,consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</div>
<div class="sections" id="projects">
<p class="header">Projects</p>
<p class="info">Projects</p>
</div>
<div class="sections" id="resume">
<p class="header">Resume</p>
<p class="info">Resume</p>
</div>
<div class="sections" id="contact">
<p class="header">Contact</p>
<p class="info">Contact</p>
</div>
</div>
</div>
解决方法
您可以创建一个外部变量,然后在def main():
updater = Updater('TOKEN',use_context=True)
conn = sqlite3.connect('list.db')
cur = conn.cursor()
mot = cur.execute("SELECT mot FROM list")
conn.commit()
dp = updater.dispatcher
dp.add_handler(CommandHandler('test',censure))
dp.add_handler(CommandHandler('add',add))
dp.add_handler(MessageHandler(mot,censure))
updater.start_polling()
updater.idle()
conn.close()
语句中对其进行检查:
if
,
PassedValue应该在滚动事件方法范围之外。每次代码调用滚动事件时,都会在滚动事件内部创建var passValue。如果将其设置为全局变量,则应该可以使用。
$f = $.noConflict();
// Moved passedValue to a global variable
var PassedValue=false;
$f(document).ready(function() {
$f(window).scroll(function() {
var scrollAmount = $f(window).scrollTop();
var documentHeight = $f(document).height();
var scrollPercent = (scrollAmount / documentHeight) * 100;
//$(".3amal").append(scrollPercent);
if(scrollAmount > documentHeight*0.3 && !PassedValue){
alert('the user scrolled once!');
PassedValue=true;
}else{
//alert('Nothing done nothing to save');
}
});
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。