javascript - 当屏幕尺寸超过一定宽度尺寸时如何运行javascript?
问题描述
我只想在窗口宽度超过 1024 时运行某些 javascript。脚本很长。我试图将它们与
if ($(window).width() >= 1024
但它仍然无法正常工作。
有人能帮我吗?
这是我想在宽度超过 1024 时运行的脚本:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(window).load(function() {
$(window).on("scroll resize", function() {
var pos = $('#fixPlace').offset();
$('.post').each(function() {
if (pos.top >= $(this).offset().top && pos.top <= $(this).next().offset().top) {
var newDescr = $(this).find('.feature__description').html();
var oldDescr = $("#fixPlace").html();
$('#fixPlace').html(newDescr);
if (newDescr !== oldDescr) {
$("#fixPlace").css('opacity', 0.4).animate({ 'opacity': '1', }, 200);
return;
}
}
});
});
$(document).ready(function() {
$(window).trigger('scroll');
});
});
let fixPlace = '#fixPlace';
let scrollStart = null;
let scrollEnd = null;
let floatBoxTop = 0.25 * $(window).height();
$(document).ready(function() {
scrollStart = $('#fixPlace').offset().top;
scrollEnd = $('.transition').offset().top - 680;
})
$(window).scroll(scroll)
function scroll() {
let scrollTop = $(document).scrollTop() + floatBoxTop;
scrollTop = Math.min(scrollEnd, Math.max(scrollTop, scrollStart))
if (scrollTop === scrollStart || scrollEnd === scrollTop) {
$(fixPlace).css({
position: 'absolute',
top: scrollTop
});
} else {
$(fixPlace).css({
position: 'fixed',
top: floatBoxTop
});
}
}
解决方案
推荐阅读
- c# - 使用 Xamarin 的 PreEmptive Dotfuscator
- jenkins - 获取java.io.IOException: CreateProcess error=2, 系统找不到jenkins中指定的文件
- jquery - jQuery最接近的方法不适用于类
- python - 基于文本文件的 for 循环迭代
- javascript - 如何修复错误:没有为“web-application/node_modules/uuid”找到有效的导出主目录?
- android - Android 通过蓝牙捕获和发送通知
- javascript - 突出显示匹配的文本
- swiftui - 创建自定义 SwiftUI 模板文件
- celery - 蟒蛇 - 芹菜
- php - MySQL - 处理 MySQL 事务查询