我正在开发一个基于jquery的主页,其中包含5个左右的隐藏div,每个div包含几个背景css图像.
问题是浏览器不会将css图像加载到DOM中,直到显示父图层的可见性,导致图像在图层变得可见时缓慢加载.
我已经考虑过的解决方案:
通过js预加载图像:
$(function() {
function preloadImg(image) {
var img = new Image();
img.src = image;
}
preloadImg('/images/home/search_bg_selected.png');
});
Run Code Online (Sandbox Code Playgroud)
这个解决方案似乎将图像加载到dom中两次......一旦js加载它,然后再次加载它的div层变得可见...所以它进行2次HTTP调用,因此不起作用.
我缺少这个问题的任何其他解决方案?
我正在使用自定义设置页面构建WordPress主题.某些设置要求用户上传/添加一组图像(超过1个).媒体上传器的默认行为是上传和/或选择单个图像并将其插入帖子中.
我遵循了这个关于利用媒体上传器的优秀指南,它表明我应该能够将多个设置为true,但它仍然只允许上传或选择单个文件.
这是我的代码:
加载所需的脚本,因为这是一个自定义设置页面:
if(function_exists( 'wp_enqueue_media' )){
wp_enqueue_media();
}else{
wp_enqueue_style('thickbox');
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
}
Run Code Online (Sandbox Code Playgroud)
Javascript/jQuery用于显示媒体上传器并处理所选图像:
var tgm_media_frame;
$('.upload-images').click(function() {
if ( tgm_media_frame ) {
tgm_media_frame.open();
return;
}
tgm_media_frame = wp.media.frames.tgm_media_frame = wp.media({
multiple: true,
library: {
type: 'image'
},
});
tgm_media_frame.on('select', function(){
var selection = tgm_media_frame.state().get('selection');
selection.map( function( attachment ) {
attachment = attachment.toJSON();
console.log(attachment);
// Do something with attachment.id and/or attachment.url here
});
});
tgm_media_frame.open();
});
Run Code Online (Sandbox Code Playgroud)
有没有人能够让多个文件选择正常工作?我错过了什么吗?谢谢!
我有一个看起来像这样的DOM结构:
<div class="chapter">
<section></section>
<section></section>
</div>
<div class="chapter">
<section></section>
<section></section>
<section class="current"></section>
<section></section>
</div>
<div class="chapter">
<section></section>
<section></section>
<section></section>
<section></section>
</div>
Run Code Online (Sandbox Code Playgroud)
我希望在所有部分中获得具有"当前"类的部分的索引号.在这个例子中,索引将是5,因为它是第5节标记并包含一类当前.
我如何使用jQuery获得这个?
我开始用这个来获取dom中所有部分的列表:
var sections = $('section');
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的事情来尝试获得我想要的结果:
alert($(sections+'.current').index());
Run Code Online (Sandbox Code Playgroud)
这会返回一个js错误.
我在这里错过了什么?谢谢!