使用AJAX加载图像时替换CSS背景

时间:2017-10-03 08:35:56

标签: javascript jquery css ajax get

我有一个占位符背景图片,我想在 之后将其替换为

$.get("BIG-IMAGE-URL").done(function(data){
    $('.MY-DIV-CLASS').css('background-image', 'url("BIG-IMAGE-URL")');
});

有谁知道如何制作这样一个简单的功能?

1 个答案:

答案 0 :(得分:0)

您可以使用javascript加载大图像,并在加载事件处理程序中设置CSS背景。

public class NewsAdapter extends FirebaseListAdapter<News> {

    public NewsAdapter(Activity activity, Class<News> modelClass, int modelLayout, Query ref) {
           super(activity, modelClass, modelLayout, ref);
    }

    @Override
    protected void populateView(View view, final News news, int i) {

      final Context context = view.getContext();

      final String title = news.getTitle();
      final String photoUrl = news.getPhotoUrl();

      // Create views and assign values
      ImageView ivPhoto = (ImageView) view.findViewById(R.id.iv_news_photo);

      if (photoUrl != null)
        // Load the image using Glide
        Glide.with(context) 
                .load(photoUrl)
                .into(ivPhoto);

      TextView tvTitle = (TextView) view.findViewById(R.id.tv_news_title);
      tvTitle.setText(title)
   }
}

来源:MDN Using_images

此外,这是使用jQuery How can I check if a background image is loaded?

的类似解决方案