简体   繁体   English

如何将图像放入我的滚动文本js

[英]How to put image into my scrolling text js

I'm using the below js plugin to do my text scrolling: https://github.com/aamirafridi/jQuery.Marquee我正在使用下面的 js 插件来滚动我的文本: https : //github.com/aamirafridi/jQuery.Marquee

Basically we just call the below for text scrolling:基本上我们只是调用下面的文本滚动:

$('.marquee').marquee();

But I plan to put image into the scrolling text too.但我也计划将图像放入滚动文本中。 Like what it mentioned in the readme file, we need to put the $(window).load like below so that it can calculate the image width accurately:就像readme文件中提到的那样,我们需要像下面这样放置$(window).load,以便它可以准确计算图像宽度:

$(window).load(function() { $('.marquee').marquee(); });

This function will load the marquee after the webpage load out.此函数将在网页加载后加载选取框。

But what if I only load the marquee after I received a command.但是,如果我只在收到命令后才加载选取框呢? For example:例如:

receivedCom = function(){ $('.marquee').marquee({duration: 8000}); });

I have try putting $(window).load into the receivedCom function.我尝试将 $(window).load 放入 receivedCom 函数中。 It won't run.它不会运行。 Any idea how?知道怎么做吗?

I reworked the example a bit more.我重新修改了这个例子。 Since you'll know the number of images you're sending from the server, you can pass that number to the client as num_images .由于您将知道从服务器发送的图像数量,因此您可以将该数字作为num_images传递给客户端。 The client will dump the marquee content into the div with marquee class and then check to see if the images expected match the number found inside the marquee div .客户端会将选取框内容转储到带有marquee类的div ,然后检查预期的图像是否与选取框div找到的数字匹配。 If not, the loading is still happening and we keep checking every half second.如果没有,加载仍在发生,我们每半秒检查一次。 Once loading has finished, we clear the setInterval call and tell the marquee to begin.加载完成后,我们清除setInterval调用并告诉选取框开始。

As we discussed in chat, setTimeout is effective when starting the marquee code because it executes the code in a separate event loop, giving the plugin a chance to "see" the newly loaded dynamic images.正如我们在聊天中所讨论的, setTimeout在启动选取框代码时是有效的,因为它在单独的事件循环中执行代码,让插件有机会“看到”新加载的动态图像。 If the following isn't working for you, try increasing the setTimeout delay slightly.如果以下方法对您不起作用,请尝试稍微增加setTimeout延迟。

I've added a working example which doesn't use socket.io.我添加了一个使用 socket.io 的工作示例 In this pen, content is dynamically added to the marquee div , two images at a time.在这支笔中,内容被动态添加到选取框div ,一次添加两个图像。 The marquee won't initialize until it sees eight images inside of it.选取框不会初始化,直到它看到其中的八个图像。 Similar to the code below, setInterval is called to check if the expected number of images matches the actual number of images in the DOM.与下面的代码类似,调用setInterval来检查预期的图像数量是否与 DOM 中的实际图像数量匹配。

HTML HTML

<div class='marquee'></div>

Server JS服务器JS

// Send the HTML along and the known number of images
// inside the string
socket.emit('your_event_name', { 
  marquee_content : 'html images and text go here',
  num_images      : 3
});

Client JS客户端JS

var socket          = io.connect('http://localhost')
    images_expected = 0, // 'num_images' from socket.emit data
    interval;

function startMarquee() {
  $('.marquee').marquee({duration: 8000});  
}

function checkImagesLoaded() {
  if ($('.marquee img').length === images_expected) {
    clearInterval(interval);
    setTimeout(startMarquee); // fire in new event loop    
  }
}

function insertMarqueeContent(content) {
  $(".marquee").html(content);
}

function initMarquee() {
  interval = window.setInterval(checkImagesLoaded, 500);  
}

socket.on('your_event_name', function (data) {
  images_expected = data.num_images;
  insertMarqueeContent(data.marquee_content);
  initMarquee();
});

If i understand your question correctly, 'after I received a command' means you are looking for an event.如果我正确理解您的问题,“在我收到命令之后”意味着您正在寻找一个事件。

There are lots of techniques depending on the type of event you want to listen to.根据您要收听的事件类型,有很多技巧。

For example, you might want to listen for a mouseclick :例如,您可能想要监听 mouseclick :

$(document).ready(function(){
  $(window).click(function(event, target){
    $('.marquee').marquee();
  });
});

JSfiddle here : http://jsfiddle.net/knr2qzxu/ If you want to play around with it a bit :) JSfiddle 在这里: http : //jsfiddle.net/knr2qzxu/如果你想玩一下:)

I strongly suggest you read more about the jQuery events here if you want to have a deeper understanding on what's going on here : https://api.jquery.com/category/events/如果您想更深入地了解这里发生的事情,我强烈建议您在此处阅读有关 jQuery 事件的更多信息: https : //api.jquery.com/category/events/

EDIT :编辑 :

Ok I went through your question again, correct me if I don't get it correctly :好的,我再次回答了您的问题,如果我没有正确理解,请纠正我:

You somewhere in your js code have a function which is written as such : receivedCom = function(com) { };你在你的 js 代码的某个地方有一个函数,它是这样写的:receiveCom = function(com) { }; If so just check if the window has been loaded before you call the marquee function :如果是这样,只需在调用选框函数之前检查窗口是否已加载:

var receivedCom = function(com) {
  if (document.readyState === "complete") {        
    $('.marquee').marquee(); 
  }
};

And there you go : The js checks if your window is displayed correctly, then triggers the marquee.然后你去:js 检查你的窗口是否正确显示,然后触发选取框。

You can also use a setInterval to check every n miliseconds if the window has loaded, and then trigger the marquee, please read the doc if you need to do this : http://www.w3schools.com/jsref/met_win_setinterval.asp如果窗口已加载,您还可以使用 setInterval 每 n 毫秒检查一次,然后触发选取框,如果您需要这样做,请阅读文档: http : //www.w3schools.com/jsref/met_win_setinterval.asp

Please tell me if it worked !请告诉我它是否有效!

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

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