简体   繁体   English

使用JavaScript优化Tumblr的加载以自定义样式

[英]Optimize the loading of my Tumblr with JavaScript to customize styles

-- Hi ! -嗨!

Reader, if you are not comfortable with Tumblr, please tell me, I will give more details! 读者,如果您对Tumblr不满意,请告诉我,我将提供更多详细信息!

I had some issues on my tumblr page with the default theme 'Optica'. 我在tumblr页面上遇到一些默认主题为“ Optica”的问题。 When I post an embed SoundCloud track, here is what I get on the page: 当我发布嵌入的SoundCloud曲目时,这是我在页面上看到的内容:

photo below 下图

NOTE: I have tried to change the theme, and I discovered that for some of them, everything goes fine, but there are a few others with the same bug 注意:我尝试更改主题,但发现其中的某些功能一切正常,但是还有其他一些具有相同的错误

So I've tried to fix that, and I've located the origin of the bug in the styles sheet main.css: 因此,我尝试解决此问题,并在样式表main.css中找到了错误的来源:

.video .video-wrapper {
background: #111;
position: relative;
padding-bottom: 56%;
height: 0;
}

The thing is, that to publish an embed code, you have to do it through the "Video post" tool on your tumblr dashboard, which then allows to style the embed code with the .video-wrapper 事实是,要发布嵌入代码,您必须通过tumblr仪表板上的“视频发布”工具进行操作,然后该工具才能使用.video-wrapper设置嵌入代码的样式

Of course, what I want to do here is reducing the bottom padding, but if I do it that way, it will affect all "Video posts". 当然,我想在这里做的是减少底部填充,但是如果这样做,它将影响所有“视频帖子”。 So I used the existing class .soundcloud_audio_player in the iframe to "mark" this type of Video posts to be able to change the styling only of these Video posts. 因此,我使用iframe中现有的类.soundcloud_audio_player来“标记”这种类型的视频帖子,以便仅更改这些视频帖子的样式。

I have simplified it, and here is what it looks like at the end in browser: 我已经简化了它,这就是浏览器结尾处的样子:

<article class="audio not-page post-70801339374 " data-post-id="post_70801339374">
<div class="post-wrapper clearfix">
    <section class="post">
        <figure>
            <div class="video-wrapper">
                <iframe class="soundcloud_audio_player" />
            </div>
        </figure>
        <section class="attribution-tags clearfix">
            <ul>
                <li>
                    <a href="http://hymced.tumblr.com/tagged/SoundCloud" class="tag">SoundCloud</a>
                </li>
                <li>
                    <a href="http://hymced.tumblr.com/tagged/Blutch" class="tag">Blutch</a>
                </li>
            </ul>
        </section>
    </section>
    <section class="panel" />
</div>
<article>

And now, what I have put between tags in my Tumblr customized HTML: 现在,我在Tumblr自定义HTML中的标签之间添加了以下内容:

<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.3");
google.setOnLoadCallback(function() {
    jQuery(function($) {
        $("iframe.soundcloud_audio_player").parents('div.video-wrapper').css("padding-bottom", "17%");
    });
});
</script>

I used jQuery because I didn't know anything about JavaScript or jQuery before today, and I thought it what simpler! 我使用jQuery是因为我在今天之前对JavaScript或jQuery一无所知,而且我认为它更简单! But maybe I'm wrong. 但是也许我错了。 Anyway, PROBLEM #1 FIXED! 无论如何,问题1已解决!

NOW, PROBLEM #2 现在,问题2

When the page is loading, the former styling (before than JS intervene) appears for 1 or 2 sec (with my bandwidth anyway!) and it's quite ugly: 页面加载时,以前的样式(比JS干预要早)显示1或2秒钟(无论如何都占用我的带宽!),而且非常难看:

photo below 下图

I've looked a bit here http://www.memodev.com/wiki/Optimiser_un_site_Web and here https://developers.google.com/speed/docs/best-practices/rules_intro?hl=fr-FR 我在这里http://www.memodev.com/wiki/Optimiser_un_site_Web和这里https://developers.google.com/speed/docs/best-practices/rules_intro?hl=fr-FR

But I can't find how to improve the loading, and I hope some of you guys can :) 但是我找不到如何提高负载的方法,希望你们中的一些人可以:)

PageSpeed test below 下面的PageSpeed测试

It sounds like you just want to hide the fact that styles are changing after the page loads - So, this will do the trick very basically : 听起来您只是想隐藏页面加载后样式正在改变的事实-因此,这基本上可以解决问题:

Add this to your HTML: <div id="screen"></div> 将此添加到您的HTML: <div id="screen"></div>

Add this to your CSS : 将此添加到您的CSS中:

div#screen {
     position:fixed;
     height:100%; width:100%; 
     background:white;
     z-index:999;
 } 

You might not need the z-index There are lots of loaders you can use in place of this, if you would like - 您可能不需要z-index。如果您愿意,可以使用很多装载程序来代替z- index-

and then, In JQuery: 然后,在JQuery中:

$(document).ready(function(){ 
    // Here, you insert that code that adds the padding to your iframe, and whatever else
    // you need to do to prepare the page... 
    $("#screen").hide();   // this will display your page - ! 
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM