如何在显示前的页面时自动滚动到底部

时间:2016-04-22 05:08:22

标签: javascript jquery css chat

我们正在开发聊天应用程序,因为我在内容div中显示图像,音频和文本。

我们有标题div,内容div,页脚div。在内容div中动态添加新数据我希望每次都显示在底部。如果想看到以前的数据,我想向下滚动查看。如何使用jquery

来做到这一点

$(document)
		.on(
				"pagebeforeshow",
				"#chat",
				function() {
					window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
		onFileSystemSuccess, fail);

		load = true;
		$('#opchat').empty();
		$("#preopchat").empty();
		$("#opchat1").empty();
		var checkData = storageChat.getItem("chatdata");
		if (JSON.parse(checkData) != null
				&& JSON.parse(checkData) != undefined) {
			storeChatData = JSON.parse(checkData).slice();
			if (storeChatData.length > 10) {
				var sliceChatData = [];
			sliceChatData = storeChatData.slice(Math.max(
				storeChatData.length - 10, 1));
			for (var i = 0; i < sliceChatData.length; i++) {
				if (sliceChatData[i] != undefined) {
				if (sliceChatData[i].startsWith("file:///")) {
			var filepath = sliceChatData[i].split(",")
			if (filepath[1] == 'image/jpeg') {
			var storedChat = '<img class="popphoto" id="profImgDashBrd"	style="height: 70px; width: 70px;" src=\''
													+ sliceChatData[i]
													+ '\'  >';
											previewData(storedChat);
		} else if (filepath[1] == 'audio/mpeg') {
						var storedChat = "<audio controls>"
							+ "<source src='"
								+ sliceChatData[i]
								+ "' type='video/mp4'>"
						+ "</audio>" + "<br>";
					previewData(storedChat);
			} else {
		var filepath = storeChatData[i].split(",")	
			var storedChat ="<div class='right'>" + "<p>"
			+ filepath[1] + "</p>" + "</div";
			$("#opchat1").append(storedChat);
			}

			} else {
		var storedChat ="<div class='left'>" + "<p>"
				+ sliceChatData[i] + "</p>" + "</div>" + "<br>";
			$("#opchat").append(storedChat);

					}
				}
			}
		} else {
			for (var i = 0; i < storeChatData.length; i++) {
			if (storeChatData[i] != undefined) {
			if (storeChatData[i].startsWith("file:///")) {
			var filepath = storeChatData[i].split(",")
			if (filepath[1] == 'image/jpeg') {
			var storedChat = '<img class="popphoto" style="height: 70px; width: 70px;" src=\''
					+ filepath[0] + '\'  >';
					previewData(storedChat);
					} else if (filepath[1] == 'audio/mpeg') {
			var storedChat = "<audio controls>"
					+ "<source src='"
			+ filepath[0]
		+ "' type='video/mp4'>"
			+ "</audio>" + "<br>";
						previewData(storedChat);
			} else {
			var filepath = storeChatData[i].split(",");
			var storedChat ="<div class='right'>" + "<p>"
			+ filepath[1] + "</p>" + "</div";
			$("#opqicchat1").append(storedChat);
											
				}
			} else {
	var storedChat ="<div class='left'>" + "<p>"
			+ storeChatData[i] + "</p>" + "</div>" + "<br>"
			$("#opqicchat").append(storedChat);
			}
			}
			}
	}
			}
				});

function onFileSystemSuccess(fileSystem) {
	folderName = "QLM"
	var directoryEntry = fileSystem.root;
	directoryEntry.getDirectory(folderName, {
		create : true,
		exclusive : false
	}, gotDirEntry, fail)

}

function gotDirEntry(dirEntry) {

	dirEntry.getFile("newFile.txt", {
		create : true,
		exclusive : false
	}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
	return true;
}

function fail() {
	alert("error code");
}

// Text ,audio and images Display in chat
function previewData(data) {
	var storedChat ="<div class='right'>" + "<p>" + data + "</p>" + "</div>" + "<br>";
	$("#opqicchat1").append(storedChat);
}

// Text enter and submit
$(document).on('click', '#btnchatsend', function() {
	var txtareaId = "txtarchat";
	var txtareaVal = $("#" + txtareaId).val();
	if (txtareaVal != null && txtareaVal != undefined && txtareaVal != "") {
		storeChatData.push("file:///" + "," + txtareaVal);
		storageChat.setItem("chatdata", JSON.stringify(storeChatData));
		previewData(txtareaVal)
	}
	$("#" + txtareaId).val("");
	$(textarea).val("");
});

// record audio and store
function startRecord() {
	navigator.device.capture.captureAudio(captureSuccess, captureError, {
		limit : 1
	});

}

function captureSuccess(e) {
	var audiofile = e[0].localURL;
	var audiofilePath = e[0].fullPath;
	audioname = audiofile.substr(audiofile.lastIndexOf('/') + 1);
	filename = Date.now().toString() + audioname;
	window.resolveLocalFileSystemURL(audiofile, copyFilePath, fail);
}
function captureError(e) {
	alert(JSON.stringify(e));

}

function playRecAudio(url) {
	if (play == true)
		audioRecord = new Media(url, onAudioSuccess, onAudioError);
	if (play == true) {
		audioRecord.play();
		play = false;
	} else {
		audioRecord.pause();
		play = true;
	}
}
function onAudioSuccess() {
}
function onAudioError() {
}

// capture image from camera

function getCapImg(source) {
	navigator.camera.getPicture(onSavedDocURISuccess, onFails, {
		destinationType : navigator.camera.DestinationType.FILE_URI,
		sourceType : source,
		saveToPhotoAlbum : true
	});
}

function imageGallery(source) {
	navigator.camera.getPicture(onSavedDocURISuccess, onFail, {
		quality : 30,
		targetWidth : 600,
		targetHeight : 600,
		destinationType : destinationType.FILE_URI,
		sourceType : source
	});
}
function onSavedDocURISuccess(imageURI) {
	filename = imageURI.substr(imageURI.lastIndexOf('/') + 1);
	filename = Date.now().toString() + filename;
	window.resolveLocalFileSystemURI(imageURI, copyFilePath, fail);

}
function copyFilePath(fileEntry) {
	window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {
		fileSys.root.getDirectory(folderName, {
			create : true,
			exclusive : false
		}, function(dir) {
			fileEntry.copyTo(dir, filename, onCopySuccess, fail);
		}, fail);
	}, fail);
}
function onCopySuccess(entry) {
	entry
			.file(function(file) {
				storeChatData.push(entry.toURL() + "," + file.type);
				storageChat.setItem("chatdata", JSON
						.stringify(storeChatData));
				var getimageData = storageQlmChat.getItem("chatdata");
				var arrayformate = JSON.parse(getimageData);

				$
						.each(
								arrayformate,
								function(index, value) {
									var imagepath = value.split(",")
									if (index == arrayformate.length - 1) {
										if (file.type == 'image/jpeg') {
											var storedChat = '<img class="popphoto" style="height: 70px; width: 70px;" src=\''
													+ imagepath[0] + '\'  >';
											previewData(storedChat);
										} else if (file.type == 'audio/mpeg') {
											var storedChat = "<audio controls>"
													+ "<source src='"
													+ imagepath[0]
													+ "' type='video/mp4'>"
													+ "</audio>" + "<br>";
											previewData(storedChat);
										}

									}
								});
			});
}

function fail(error) {
	alert("error" + error.code);
}
.left {
  margin-top:10px;
  position: relative;
  background: aqua;
  text-align: left;
  min-width: 85%;
  padding: 15px 10px;
  border-radius: 6px;
  border: 1px solid #ccc;
  float: left;
  left: 8%;
}

.left::before {
  content: '';
  position: absolute;
  visibility: visible;
  top: -1px;
  left: -10px;
  border: 10px solid transparent;
  border-top: 10px solid #ccc;
}

.left::after {
  content: '';
  position: absolute;
  visibility: visible;
  top: 0px;
  left: -8px;
  border: 10px solid transparent;
  border-top: 10px solid aqua;
  clear: both;
}


 .right {
  position: relative;
  background: white;
  text-align: right;
  min-width: 85%;
  padding: 10px 15px;
  border-radius: 6px;
  border: 1px solid #ccc;
  float: right;
  right: 5%;
  margin-top: 10px;
} 

.right::before {
  content: '';
  position: absolute;
  visibility: visible;
  top: -1px;
  right: -10px;
  border: 10px solid transparent;
  border-top: 10px solid #ccc;
}

.right::after {
  content: '';
  position: absolute;
  visibility: visible;
  top: 0px;
  right: -8px;
  border: 10px solid transparent;
  border-top: 10px solid white;
  clear: both;
}
<div class="headerDiv" data-role="header"
	id="hdrIdchat" data-position="fixed" data-tap-toggle="false" data-transition="none">
	<a class="ui-btn ui-icon-arrow-l ui-btn-icon-left" onClick="navBack()"></a>
	<h1 id="lblHdrchat">Chat</h1>

	<div data-tap-toggle="false" data-transition="none"  style="border-top:1px solid white;width: 100%; text-align: center; display: inline-flex;">		
		<div style="width: 100%; padding: 10px;">
				<img alt="" src="images/vo.png" onclick="startRecord()" style="height: 25px;">
			</div>
			<div style="width: 100%; padding: 10px">
				<img alt="" src="images/imcht.png" onClick="imageGallery(pictureSource.PHOTOLIBRARY)" style="height: 25px;">
			</div>
			<div style="width: 100%; padding: 10px">
				<img alt="" src="images/camcht.png" onclick="getCapImg(navigator.camera.PictureSourceType.CAMERA);" style="height: 25px;">
		</div>
	</div>
</div>

<div data-role="content">


	<div onclick="preChatData()">
		<p align="center"></p>
	</div>

	<div id="example">
		<div id="preopqicchat"></div>
		<div id="opqicchat" ></div>
		<div id="opqicchat1" ></div>
	</div>
</div>

<div data-role="footer" data-position="fixed" id="chatfooter"
	style="background: transparent; border: none;" data-tap-toggle="false" data-transition="none">
	<div style="display: inline-flex; width: 100%;">
		<div style="width: 100%">
			<textarea autofocus="autofocus" id="txtarchat"></textarea>
		</div>
		<div>
			<button data-role="button" class="ui-btn ui-btn-b"
				id="btnchatsend" style="width: 100% !Important;">SEND</button>
		</div>
	</div>
</div>

Actual Result

Expected Result

1 个答案:

答案 0 :(得分:0)

看看这个demo

header,
footer {
  flex: 0 0 auto;
}

main随着内容的增长而增加。号码&#39; 1&#39;在flex中,简写说明容器中有多少可用空间分配给元素。在我们的例子中,主要给予自由空间。值的自动部分是flex元素的默认大小。我们想要所有弹性儿童的自动。

main {
  flex: 1 0 auto;
  ...
}

无论如何,页脚始终位于内容之下。

页脚从底部开始

enter image description here

按内容推送页脚

enter image description here

http://codepen.io/antibland/pen/WwKRBx