iframe CSS覆盖新的Twitter小部件

321*_*321 13 html javascript css iframe

我正在尝试自定义新的Twitter小部件,但是在CSS上遇到了一些问题,似乎无法覆盖这个新的小部件.我试过寻找解决方案,但找不到任何匹配.

这是我的页面右侧导航中的小部件和.timeline类的调整后的CSS:

.timeline {
margin-bottom:0;
border:0 !important;
border-radius:0 !important;
-webkit-border-radius:0;
-moz-border-radius:0;
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<div class="bg">
<h3 class="twitter">Twitter News</h3>
<div id="twitter">
<a class="twitter-timeline" href="https://twitter.com/" data-widget-id="268336500536647680">Tweets by </a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
</div></div>
Run Code Online (Sandbox Code Playgroud)

我已经开始使用!important声明并尝试使用主#twitter id来覆盖,但都不起作用.

任何想法都非常感激.

**我显然需要以某种方式编辑iframe,但不确定如何 - 如果有人更清楚地知道如何使用我的代码(而不是教程)来做到这一点会很棒.我还需要更改iframe宽度..

Seb*_*man 26

我设法使用javascript(我在我的应用程序中使用jQuery!).您必须在iframe加载后应用样式(我没有找到有效的"已加载"事件,因此我使用了轮询策略).

// Customize twitter feed
var hideTwitterAttempts = 0;
function hideTwitterBoxElements() {
    setTimeout( function() {
        if ( $('[id*=twitter]').length ) {
        $('[id*=twitter]').each( function(){
            if ( $(this).width() == 220 ) {
                $(this).width( 198 ); //override min-width of 220px
            }
            var ibody = $(this).contents().find( 'body' );
            ibody.width( $(this).width() + 20 ); //remove scrollbar by adding width

            if ( ibody.find( '.timeline .stream .h-feed li.tweet' ).length ) {
            ibody.find( '.timeline' ).css( 'border', 0 );
            ibody.find( '.timeline .stream' ).css( 'overflow-x', 'hidden' );
            ibody.find( '.timeline .stream' ).css( 'overflow-y', 'scroll' );
            ibody.find( '.timeline-header').hide();
            ibody.find( '.timeline-footer').hide();
            }
            else {
                $(this).hide();
            }
        });
        }
        hideTwitterAttempts++;
        if ( hideTwitterAttempts < 3 ) {
            hideTwitterBoxElements();
        }
    }, 1500);
}

// somewhere in your code after html page load
hideTwitterBoxElements();
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用此处定义的函数http://stackoverflow.com/a/13709125/893510执行:$("iframe#twitter-widget-0").waitUntilExists(function(){// Formatting}); (5认同)

Cam*_*rzt 7

这对任何感兴趣的人都没有jQuery.

更新了更好的解决方案,不依赖于轮询,因此偶尔没有无格式布局的闪光:

twttr.events.bind('rendered',function(){
  [].slice.call(document.querySelectorAll('iframe.twitter-timeline')).forEach(function(e,i,a){
    var fE = e.contentDocument.getElementsByClassName('timeline-footer');
    if(fE.length){
      fE[0].style.backgroundColor='transparent';
    }
  });
});
Run Code Online (Sandbox Code Playgroud)

旧版:

// Customize twitter feed                                                                              
var hideTwitterAttempts = 0;                                                                           
function hideTwitterBoxElements() {                                                                    
    setTimeout( function() {                                                                           
        var list = document.getElementsByTagName('iframe');                                            
        if (list.length ) {                                                                            
            Array.prototype.forEach.call(                                                              
                list,                                                                                  
                function(element){                                                                     
                    var footerE = element.contentDocument.getElementsByClassName('timeline-footer')[0];
                    footerE.style.backgroundColor="transparent";                                       
                });                                                                                    
        }                                                                                              
        hideTwitterAttempts++;                                                                         
        if ( hideTwitterAttempts < 3 ) {                                                               
            hideTwitterBoxElements();                                                                  
        }                                                                                              
    }, 1500);                                                                                          
}                                                                                                      
hideTwitterBoxElements();                                                                              
Run Code Online (Sandbox Code Playgroud)


KPK*_*KPK 6

我调整了来自@Sebastiaan Ordelman的代码并添加了一些注释(指旧API的值).这是我尝试在短时间内匹配旧API.将此代码放在新的复制并粘贴新的Twitter小部件代码之后.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
var hideTwitterAttempts = 0;
function hideTwitterBoxElements() {
    setTimeout( function() {
        if ( $('[id*=twitter]').length ) {
        $('[id*=twitter]').each( function(){
            var ibody = $(this).contents().find( 'body' );

            if ( ibody.find( '.timeline .stream .h-feed li.tweet' ).length ) {
            ibody.find( '.customisable-border' ).css( 'border', 0 );
            ibody.find( '.timeline' ).css( 'background-color', '#004A7B' ); //theme: shell: background:
            ibody.find( 'ol.h-feed' ).css( 'background-color', '#0575A1' ); //theme: tweets: background:
            ibody.find( 'ol.h-feed' ).css( 'border-radius', '5px 5px 5px 5px' );
            ibody.find( 'li.tweet' ).css( 'border-bottom', '1px dotted #FFFFFF' ); //theme: tweets: color:
            ibody.find( 'li.tweet' ).css( 'color', '#FFFFFF' ); //theme: tweets: color:
            ibody.find( '.customisable:link' ).css( 'color', '#07E0EB' ); //theme: tweets: links:
            ibody.find( '.footer' ).css( 'visibility', 'hidden' ); //hide reply, retweet, favorite images
            ibody.find( '.footer' ).css( 'min-height', 0 ); //hide reply, retweet, favorite images
            ibody.find( '.footer' ).css( 'height', 0 ); //hide reply, retweet, favorite images
            ibody.find( '.avatar' ).css( 'height', 0 ); //hide avatar
            ibody.find( '.avatar' ).css( 'width', 0 ); //hide avatar
            ibody.find( '.p-nickname' ).css( 'font-size', 0 ); //hide @name of tweet
            ibody.find( '.p-nickname' ).css( 'visibility', 'hidden' ); //hide @name of tweet
            ibody.find( '.e-entry-content' ).css( 'margin', '-25px 0px 0px 0px' ); //move tweet up (over @name of tweet)
            ibody.find( '.dt-updated' ).css( 'color', '#07E0EB' ); //theme: tweets: links:
            ibody.find( '.full-name' ).css( 'margin', '0px 0px 0px -35px' ); //move name of tweet to left (over avatar)
            ibody.find( 'h1.summary' ).replaceWith( '<h1 class="summary"><a class="customisable-highlight" title="Tweets from fundSchedule" href="https://twitter.com/fundschedule" style="color: #FFFFFF;">fundSchedule</a></h1>' ); //replace Tweets text at top
            ibody.find( '.p-name' ).css( 'color', '#07E0EB' ); //theme: tweets: links:
            }
            else {
                $(this).hide();
            }
        });
        }
        hideTwitterAttempts++;
        if ( hideTwitterAttempts < 3 ) {
            hideTwitterBoxElements();
        }
    }, 1500);
}

// somewhere in your code after html page load
hideTwitterBoxElements();
</script>
Run Code Online (Sandbox Code Playgroud)

变化的形象:http://img837.imageshack.us/img837/1139/btjc.png


pti*_*tim 5

这并没有直接回答问题的CSS方面,但我认为值得注意的是,你想要的一些东西(例如隐藏边框/标题)可以使用窗口小部件中传递的数据属性来完成:

<a class="twitter-timeline" href="https://twitter.com/twitterapi" 
    data-widget-id="YOUR-WIDGET-ID-HERE" 
    data-theme="dark"
    data-chrome="noheader nofooter noborders noscrollbar transparent"
    data-link-color="#cc0000"
    width="200"
    height="300"
    >Tweets by @twitterapi</a>
Run Code Online (Sandbox Code Playgroud)

https://dev.twitter.com/docs/embedded-timelines#customization

也许在可能的情况下最大限度地减少javascript arm wrestling有一个性能优势?


Gab*_*oli 3

他们创建了一个 iframe,所以它是另一个文档。

您无法在另一个文档中设置元素的样式,因此这是不可能的,除非 twitter 提供了一个挂钩来将您自己的样式表嵌入到其 iframe 中。

Twitter 只允许设置小部件链接颜色的样式并选择浅色/深色主题。


或者,您可以使用twitter API创建您自己的小部件,然后您可以按照您想要的方式设置它的样式。